New Posts  All Forums:Forum Nav:

Java: sort parallel arrays

post #1 of 6
Thread Starter 
For my Java final project, I have to create a program that will allow a user to sort and search through a list of CDs:


I have put them into two 2-dim arrays:
Code:
String[][] first = { {"AC/DC", "Plug me In"},
                         {"Beatles", "Yellow Submarine"},
                         {"Creed", "Human Sacrifice"},
                         {"U-2", "The Joshua Tree"},
                         {"Queen", "Say It's Not True"},
                         {"Eric Clapton", "Rush"},
                         {"MatchBox 20", "Bent"},
                         {"Aero Smith", "Just Push Play"},
                         {"Chris Isaak", "Best of Chris Isaak"},
                         {"U-2", "Window in the Skies"},
                         {"Elton John", "Rocketman"},
                         {"Tool", "The Lowdown"}
                         {"Queen", "Another One Bites the Dust"},
                         {"MatchBox 20", "Matchbox 20"},
                         {"Foo Fighters", "The Pretender"}
                         {"Eric Clapton", "Tears in Heaven"};
                       }
                       
int[][] second = {{101,2},
          {2, 5},
                      {56, 23},
                      {60, 23},
                      {34, 7},
                      {5, 26},
                      {15, 9},
                      {42, 12},
                      {31, 11},
                      {60, 24},
                      {18, 18},
                      {13, 1}
                      {24, 8},
                      {15, 6},
                      {16, 19}
                      {22, 20};
     }
My problem is, how do I sort one (by artist name, bin number, etc.) array and keep the other in sync? Also, I would probably be easiest to put this in a single method, but how would I tell it how to sort?
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
post #2 of 6
Use the [code] tags if you want to keep everything in order on the forums

I'd help you but I don't know jack about Java.
    
CPUMotherboardGraphicsRAM
Q8400 DFI LP DK P45-T2RS eVGA GTX 650 Kingston 
Hard DriveOptical DriveOSMonitor
2.8TB Total LG Super DVD Burner SATA VII Enterprise x64 Gateway FHD2401 
PowerCaseMouseMouse Pad
Corsair VX550W NZXT Source 210 White Logitech G400 Razer Kabuto 
Audio
Creative Audigy 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q8400 DFI LP DK P45-T2RS eVGA GTX 650 Kingston 
Hard DriveOptical DriveOSMonitor
2.8TB Total LG Super DVD Burner SATA VII Enterprise x64 Gateway FHD2401 
PowerCaseMouseMouse Pad
Corsair VX550W NZXT Source 210 White Logitech G400 Razer Kabuto 
Audio
Creative Audigy 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by legoman786 View Post
Use the [code] tags if you want to keep everything in order on the forums

I'd help you but I don't know jack about Java.
Ah, thank you!
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
post #4 of 6
Check out these sorting algorithms.

The easiest one is called bubblesort. And all you need to do is use the same indexes between both arrays.

it goes something like:

Code:
var i;
var j;

for (i=0; i<array.length; i++)
    for (j=array.length; j>i+1; j--)
        if (array[j-1][0] > array[j][0])
        {
            swap(array[j-1], array[j]);
            swap(second_array[j-1], second_array[j]);
        }
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
post #5 of 6
Thread Starter 
So would this be correct for sorting by bin number?
Code:
private static void sortarray(int[][] array){
   
int i,j;

for (i=0; i<array.length; i++)
        for (j=array.length; j>i+1; j--)
        if (second[0][j-1] > first[0][j])
        {
                 swap(second[j-1], second[j]);
                 swap(first[j-1], first[j]);
        }
}
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
post #6 of 6
Instead of using 2 2D arrays you should use 4 1D arrays. The way it is coded now would make the code a bit more complicated, and storing in an nx2 matrix rather than a 2xn matrix can decrease performance by up to half.

The cleanest way to implement this is with a class for entries
Code:
public class CDEntry implements Comparable{
public String artist, title;
int code, bin;

public CDEntry(String a, String t, int c, int b){
artist = a; title = b;
code = c; bin = b;
}

public CDEntry(){
artist = title = null;
code = bin = 0;
}

int compareTo(Object o){
if(! (o instanceof CDEntry))
return 1;
CDEntry other = (CDEntry)o;
if(other.bin > bin) return -1;
else if(other.bin == bin) return 0;
else return 1;
}
}
Then you can create a single array of entries. Making it implement Comparable allows you to use the Arrays.sort(Object[] o) method to sort it using an optimized algorithm that you don't have to write yourself
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming