Results 1 to 6 of 6
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default Sort the rows of a file according to an integer

    Hi guys,

    I have a file with about 7'000 rows. Each row has 6 fields (words or numbers), all separated by a space, like this :

    field1 field2 field3 field4 field5 field6

    I would like to sort all these lines according to the 3rd field which is actually an "int".

    I saw that people use the class Comparator and rewrite the method "compare" inside it but I don't understand how to apply it to my program. My idea is to first read the file and store all these lines into an ArrayList and then sort this ArrayList according to the 3rd field of each member of the ArrayList.

    That's what I tried by adapting it to ArrayList instead of a HashMap but doesn't work...

    Java Code:
    static Map sortByValue(Map map) {
         List list = new LinkedList(map.entrySet());
         Collections.sort(list, new Comparator() {
              public int compare(Object o1, Object o2) {
                   return ((Comparable) ((Map.Entry) (o1)).getValue())
                  .compareTo(((Map.Entry) (o2)).getValue());
              }
         });
    
    Map result = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
         Map.Entry entry = (Map.Entry)it.next();
         result.put(entry.getKey(), entry.getValue());
         }
    return result;
    }

    The values of this 3rd field are not all distinct (i.e. 2 rows can have the same int value)

    Does anybody have an idea ?

    Thanks in advance!
    Last edited by ze snow; 02-19-2010 at 08:13 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You don't need a Map for this.
    Create a class (say called Record) that represents the data with those fields field1, field2, etc as properties and then simply load the records into a List<Record>. You then Collections.sort the list passing a Comparator that compares the 3rd field values of a Record.

  3. #3
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Ok I kind of see but do you have an example (i mean some code) to illustrate ? I'm still not that familiar with that comparator and it seems I have to override the method compare ?

    Thanks

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You can use a Column Comparator or Bean Comparator, depending on how you decide to store each row of data from the file.

  6. #6
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks both for your help!

    I chose to follow the link posted by r035198x, so I created another class and overrid the compare method inside and yes, that works!

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. Replies: 2
    Last Post: 01-26-2009, 04:28 PM
  3. file read as integer.
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 12-14-2008, 06:36 AM
  4. Replies: 3
    Last Post: 08-24-2008, 01:51 PM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •