Eckersley
Eckersley

Reputation: 89

Sort an ArrayList by first word

So, I have read a hashtable into a ArrayList for sorting purposes. I have a lot of numeric values followed by whitespace and then another number which indicates where it was found from a text file. So my unsorted Array looks something like this:

10 1
11 7
1 12
47 9
and so on. If i sort this by Collection.sort(); my array will look like this:
10 1
1 7
11 12
47 9
So it compares them alphabetically, not numerically. What I want is to ignore the second number and sort the list by the first word.

public void xor(arrayObject[] array){
    try{

    FileWriter textWriter = new FileWriter(new File("xor.txt"));
    ArrayList<String> temp = new ArrayList<>();
    String tempString;

    for(int i = 0; i < array.length; i++){

        if(array[i] != null){
            tempString ="";
            int hash = hashFunction(i);
            int length = String.valueOf(array[hash].value).length();
            if(array[hash].foundFromA && !array[hash].foundFromB){

                tempString += Integer.toString(array[hash].value);
               for(int a = 0; a < 10-length; a++){

                 tempString += " ";
                }
                tempString += "1";
                temp.add(tempString);

            }
            else if(!array[hash].foundFromA && array[hash].foundFromB){

                tempString += Integer.toString(array[hash].value);

                for(int a = 0; a < 10-length; a++){
                 tempString += " ";
                }

                tempString += "2";
                temp.add(tempString);
            }

        }
    }
    Collections.sort(temp);

    for(String s : temp){
        textWriter.write(s);
        textWriter.write(System.lineSeparator());
    }
    textWriter.close();
     System.out.println("Writing xor file succesful");
    }
    catch(IOException e){
        System.out.println("Failed to save file");
    }
}

Upvotes: 0

Views: 615

Answers (2)

Gabriel
Gabriel

Reputation: 1942

As stated by Abdou you can use a Comparator, but you can pass it directly to the sort method instead of create a separate class, which is easier imho.

import java.util.*;

public class HelloWorld {
  public static void main(String[] args) {
    ArrayList<String> myList = new ArrayList<>();

    myList.add("10 1");
    myList.add("11 7");
    myList.add("1 12");
    myList.add("47 9");
    myList.add("110 9");

    Collections.sort(myList, new Comparator<String>() {
      public int compare(String a, String b) {
        int n1 = Integer.parseInt(a.split(" ")[0]);
        int n2 = Integer.parseInt(b.split(" ")[0]);

        return n1 - n2;
      } 
    });

    for (String item : myList) {
      System.out.println(item);
    }
  }
}

Although I'd create a class for the values and let this class implement the Comparable interface. It would be cleaner and the sort method would work out of the box.

Upvotes: 1

Abdou Rayes
Abdou Rayes

Reputation: 430

you can make a comparator class and use it in the sort method

public class MyComparator implements java.util.Comparator<String> {

    public int compare(String s1, String s2) {
        return Integer.parseInt(s1.split( " " )[0]) - Integer.parseInt( s2.split( " " )[0] );
    }
}

use it like this

Collections.sort(temp, new myComparator());

Upvotes: 1

Related Questions