I try to sort a tab-delimited file based on the value of three columns. But it did not sort correctly.How can I solve this problem? I used the code written in this page .
The output is this:
clueweb09-en0000-12-00000 10722 10732 0.995358 0.000336 /m/0cbx95
clueweb09-en0000-12-00000 10736 10746 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 11230 11237 0.829546 0.000291 /m/03jm5
clueweb09-en0000-12-00000 13009 13024 0.540326 0.000085 /m/012qgt
clueweb09-en0000-12-00000 13050 13060 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 1338 1348 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 1864 1874 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 2018 2028 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 2745 2752 0.78671 0.000722 /m/02jx1
clueweb09-en0000-12-00000 2823 2829 0.956747 0.000476 /m/04jpl
clueweb09-en0000-12-00000 2856 2862 0.649632 0.000007 /m/0gs0g
I want this output:
clueweb09-en0000-12-00000 1338 1348 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 1864 1874 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 2018 2028 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 2745 2752 0.78671 0.000722 /m/02jx1
clueweb09-en0000-12-00000 2823 2829 0.956747 0.000476 /m/04jpl
clueweb09-en0000-12-00000 2856 2862 0.649632 0.000007 /m/0gs0g
clueweb09-en0000-12-00000 10722 10732 0.995358 0.000336 /m/0cbx95
clueweb09-en0000-12-00000 10736 10746 0.950789 0.000697 /m/01n7q
clueweb09-en0000-12-00000 11230 11237 0.829546 0.000291 /m/03jm5
clueweb09-en0000-12-00000 13009 13024 0.540326 0.000085 /m/012qgt
clueweb09-en0000-12-00000 13050 13060 0.950789 0.000697 /m/01n7q
write a your own comparator for the columns you want to sort.As Walter said currently you will get the data as string,Convert that to list of object and sort using comparator. Hope this helps.
The numbers are treated at this moment as strings but they are left aligned and sorted that way. You need to change the compare function to deal with this or, when you read the file transform those numbers to right aligned strings.
I solve the problem by changing the compare function of this page to the following code.
private static <T> Comparator<List<T>> createComparator(
final Comparator<? super T> delegate, final int... indices)
{
return new Comparator<List<T>>()
{
@Override
public int compare(List<T> list0, List<T> list1)
{
T element0 = list0.get(indices[0]);
T element1 = list1.get(indices[0]);
int n = delegate.compare(element0, element1);
if (n != 0)
{
return n;
}
else
return Integer.compare(Integer.parseInt(list0.get(indices[1]).toString()), Integer.parseInt(list1.get(indices[1]).toString()));
}
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.