How can i sort ArrayList in java without using comparator.?
I have already used comparator and it work not properly for me, means data is not in particuler manner.
I have one array list that contain following properties...
getTotal(),
getID(),
getRank(),
getItemName(),
getFinalRank()
I have sotred this all this into one arraylist itemWiseDetails
now i want to make poi report and display all this details but according to rank of that itemName. ANd one more thing my rank is in String so when i Tried to sort based on this rank it take N/A data as a 0 rank so it displayed first then it display first rank , then second and go on ..
So, I want to sort this itemWiseDetails list without comparator
Thanks in advance,
I have implement Comparator like this way
public int compareTo(Object itemDetailVO)
{
if (!(itemDetailVOinstanceof ItemDetailVO)) throw new ClassCastException("AItemDetailVOobject expected.");
int otherItemRank = Integer.parseInt(((ItemDetailVO) itemDetailVO).getRank().toString());
return Integer.parseInt(this.trLotRank.toString())- otherBidderRank;
}
You have two options:
Comparable
, and call Collections.sort
without specifying a comparator You seem to be taking the approach of "I tried X and it didn't work, therefore I need to try something else" - but the reason X (using a comparator) didn't work appears to be that your comparator had bugs... it didn't compare items in the way that you wanted it to. You'll run into exactly the same problem when you implement Comparable
. You've still got the same fundamental work to do (working out how to compare two items) - it's really just a matter of where that logic goes.
Generally implementing a Comparator
is a more flexible approach, in that it allows a collection to be sorted in different ways depending on your needs. It also allows you to sort collections where you can't change the code within the element class itself. I would personally stick to the Comparator
approach.
Write some unit tests comparing various items, and then implement the comparator so that those tests pass. Then if you see any more problems, add a test for that case and make that pass too. Iterate until your comparator is working properly.
Ok, let's correct (or rewrite) your comparator.
public class ItemDetailVOComparator implements Comparator<ItemDetailVO> {
public int compare(ItemDetailVO itemDetailVO1, ItemDetailVO itemDetailVO2) {
String rank1 = itemDetailVO1.getRank();
String rank2 = itemDetailVO1.getRank();
if (rank1 == null && rank2 == null) {
return 0;
}
// invert 1 and -1 if nulls should appear first
if (rank1 == null) {
return 1;
}
if (rank2 == null) {
return -1;
}
// nothing can be null at this point
return rank1.compareTo(rank2);
}
}
This is easier to read... and it works :-)
To answer the first question:
java.util.Collections.sort(itemWiseDetails);
To address your problem: Write a correct Comparator. Chances are yours did not have one of the required properties and therefore produced inconsistent results. If you need help doing that, feel free to post your (short!) data structure, sorting wishes, and current comparator in this or another question.
You can try using comparator:
public static List sort(List list) {
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[0]) );
Integer i2 = new Integer( Integer.parseInt(integer2[0]) );
if (i1.equals(i2))
return chars1[1].compareTo(chars2[1]);
else
return i1.compareTo(i2);
}
});
return list;
}
Input:
String i[] = { "115", "1125", "147", "37", "57", "37" };
Output:
37 37 57 115 147 1125
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.