Results 1 to 6 of 6
- 02-19-2010, 08:08 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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.
- 02-19-2010, 09:51 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 02-19-2010, 02:45 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
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
- 02-19-2010, 03:20 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 02-19-2010, 04:47 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You can use a Column Comparator or Bean Comparator, depending on how you decide to store each row of data from the file.
- 02-20-2010, 04:44 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
PROOF READ: Sort text file 3 different ways and compare
By VinceGuad in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:28 PM -
file read as integer.
By makpandian in forum New To JavaReplies: 4Last Post: 12-14-2008, 06:36 AM -
Writing integer pixel array(Range:0-255) into .txt file
By Mazharul in forum Java 2DReplies: 3Last Post: 08-24-2008, 01:51 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks