Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Sorting ONLY integers in an ArrayList (JAVA)
New Posts  All Forums:Forum Nav:

Sorting ONLY integers in an ArrayList (JAVA)

post #1 of 12
Thread Starter 
I'm looking for a little guidance since I am new to programming. I have an ArrayList (al) with three elements (firstName, lastName, personalRecord) stored in each instance of it. For example, I were run a get method -- al.get(0) -- it would return something like "George Bush 1000." Is it possible to use something like Collections.sort(al) and sort ONLY the integer value stored?
post #2 of 12
I'm going to take a stab at this even though I only know some c++. But supposedly java is so similar that the fundamental ideas should be the same.

My guess is that the sort method would have to create a temp array of the same length. Copy in only the integers from your original array and do the sort there. After that is done, you could copy the temp arrays modified order values back to the integer field of the original array, going index by index, but not changing the name fields.

But for practical data keeping purposes, I'm not sure how this would apply. Usually the type of multi-element array you are describing would be based on key values that would sort the data, thus the keys (personalRecord, in your case) would not separate from the names. If you would want to also be able to sort by a key, you would need a different sort method from what I described above.
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
post #3 of 12
My Java is rusty, but if I were doing it in C++, I'd override the comparison operations so that comparisons between your ArrayList elements only used the integer portion.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #4 of 12
Quote:
Originally Posted by C-bro View Post
My Java is rusty, but if I were doing it in C++, I'd override the comparison operations so that comparisons between your ArrayList elements only used the integer portion.
Yeah that makes sense and is much easier. I guess it all depends on how your methods are set up. I'm new and tend to overthink (and over complicate) things.

But wouldn't the override affect comparisons in other parts of the program as well? What if you didn't want it comparing that way later?
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
The Dust Trapper
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x3 720 BE w/4th core unlocked @ 3.2 Giga GA-MA790X-UD4P EVGA gforce GTX 260 4 1gig sticks of A-data DDR2 800 
Hard DriveOptical DriveOSMonitor
WD caviar black 640GB DVDVDVDVDV win7 64bit acer 23" 
Power
Antec earthwatt 650 
  hide details  
Reply
post #5 of 12
So you want to sort each of the elements by the age essentially?
post #6 of 12
This works, very messy though I'm sure it can be improved.
Basically creates new int array from main ArrayList, then manually sorts it and applies the sort to a String Array at the same time.
Code:
public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("George Bush 4000"); al.add("Bill McSteve 3011");al.add("test lol 3010");
        String[] strA = new String[al.size()];
        int[] lol = new int[strA.length];
        for(int x=0;x<strA.length;x++)strA[x] = (String)al.get(x);
        
        StringTokenizer readIn;
        for(int i=0;i<strA.length;i++){
            readIn = new StringTokenizer(strA[i]," ");
            while(readIn.hasMoreTokens()){
                try{
                    lol[i] = Integer.parseInt(readIn.nextToken());
                }catch(NumberFormatException nfe){
                }
            }
        }
        
        int f = lol.length;
        String temp="";
        int tmp=0;
        for(int i = 0;i<f;i++){
            for(int j=i+1;j<f;j++){
               if(lol[i] > lol[j]){
                   temp = strA[i];
                   strA[i]=strA[j];
                   strA[j]=temp;
                   tmp = lol[i];
                   lol[i]=lol[j];
                   lol[j]=tmp;
               }
            }
        }
        for(int y=0;y<strA.length;y++)out.println(strA[y]);
    }
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
post #7 of 12
You need to make a new class that extends ArrayList and implements Comparable

Than add a compareTo() method.

Then Collections.sort(al) will work.
post #8 of 12
Mind knocking up an example? I'm just teaching myself casually, it'd be good to see how one is actually meant to implement this
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
post #9 of 12
Do you mean sort all elements by their integer value, or sort only the integers and leave the other parts the the elements in the same order?
post #10 of 12
I mean as the OP described the problem, sort all elements by their integer value. How would you write it using the method you wrote three posts up, i.e. collections?
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
Blitzkrieg
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 980X Asus P6X58D Premium GTX 680 G.Skill 6Gb 2000Mhz 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 3 BD/DVD Combo EK Supreme HF Windows 7 
MonitorKeyboardPowerCase
SAMSUNG S27A850T 27" Microsoft basic 600 :p AeroCool V12 1000W HAF 932 
Mouse
Razer Deathadder V2 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Sorting ONLY integers in an ArrayList (JAVA)