Reputation: 192
I am facing a problem in sorting. The format of data is:
b4 S0_c5 t 0.426544
b6 S1_c5 t 1.51049
b13 S0_c5 t 0.594502
b13 S1_c5 t 0.537496
b15 S1_c5 t 0.884126
b18 S0_c5 t 0.500933
b19 S1_c5 t 0.628472
b22 S0_c5 t 0.437718
and required result is:
S0_c5 b13 0.594502 b18 0.500933 b22 0.437718 b4 0.426544
S1_c5 b6 1.51049 b15 0.884126 b19 0.628472 b13 0.537496
the value is also in descending order. Thanks in advance.
Upvotes: 1
Views: 213
Reputation: 4792
Try this. It will work.
private void ReadTextFile(String filename) throws IOException
{
BufferedReader br = null;
FileInputStream fin = null;
fin = new FileInputStream(filename);
br =new BufferedReader(new InputStreamReader(fin));
Map<String,String> stringStringMap = new TreeMap<String, String>(Collections.reverseOrder());
while ((line = br.readLine()) != null) {
stringStringMap.put(line.split(" ")[3],line);
}
Collection<String> collection = stringStringMap.values();
Map<String, List<String>> map = new TreeMap<String, List<String>>();
Iterator<String> iterator = collection.iterator();
while(iterator.hasNext()){
String[] tokens = iterator.next().split(" ");
List<String> values = map.get(tokens[1]);
if (values == null) {
values = new ArrayList<String>();
map.put(tokens[1], values);
}
values.add(tokens[0] + " " + tokens[3]);
}
for (List<String> mapList : map.values()) {
Collections.sort(mapList);
}
for (String key : map.keySet()) {
System.out.println(key + " " + map.get(key));
}
}
Upvotes: 1
Reputation: 7957
Put the data in a TreeList<String, List<String>>
(because it's sorted) where the second word from a sequence is the key, and the value a list of strings, then sort each list you obtain this way:
Map<String, List<String[]>> map = new TreeMap<String, List<String[]>>();
for (String s : strings) {
String[] tokens = s.split(" ");
List<String[]> values = map.get(tokens[1]);
if (values == null) {
values = new ArrayList<String[]>();
map.put(tokens[1], values);
}
values.add(new String[]{tokens[0], tokens[3]});
}
for (String key : map.keySet()) {
List<String[]> list = map.get(key);
Collections.sort(list, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[1].compareTo(o2[1]) * -1;
}
});
System.out.print(key + " ");
for (String[] s : list) {
System.out.print(s[0] + " " + s[1]);
}
System.out.println();
}
Update: E.g. to read from a file:
BufferedReader br;
try {
br = new BufferedReader(new FileReader("d:/temp/r.res"));
Map<String, List<String[]>> map = new TreeMap<String, List<String[]>>();
while (br.ready()) {
String s = br.readLine();
if (!s.trim().isEmpty()) {
String[] tokens = s.split(" ");
List<String[]> values = map.get(tokens[1]);
if (values == null) {
values = new ArrayList<String[]>();
map.put(tokens[1], values);
}
values.add(new String[]{tokens[0], tokens[3]});
}
}
} finally {
br.close();
}
Upvotes: 2
Reputation: 16060
HashMap<String, List<String[]>
>List
Upvotes: 2
Reputation: 1235
there is a class in the JDK just for the purpose of having a sorted list. It is named (somewhat out of order with the other Sorted* interfaces) "java.util.PriorityQueue". It can sort either Comparables or using a Comparator.
The difference with a List sorted using Collections.sort(...) is that this will maintain order at all times, and have good insertion performance by using a heap data structure, where inserting in a sorted ArrayList will be O(n) (i.e., using binary search and move).
However other than List, PriorityQueue does not support indexed access (get(5)), the only way to access items in a heap is to take them out, one at a time (thus the name PriorityQueue).
Upvotes: 1
Reputation: 19443
Put the data into a List
and use Collections.sort()
to sort it.
Upvotes: 1