I have a String list:
List<String> listString = new ArrayList<String>();
listString.add("faq");
listString.add("general");
listString.add("contact");
I do some processing on the list and I want to sort this list but I want "general" to always end up in first position. Thx ;)
I like @Petar's approach, but another approach would be to sort it using a custom Comparator that always said that "general" was before whatever it was being compared to.
Collections.sort(list, new Comparator<String>()
{
int compare(String o1, String o2)
{
if (o1.equals(o2)) // update to make it stable
return 0;
if (o1.equals("general"))
return -1;
if (o2.equals("general"))
return 1;
return o1.compareTo(o2);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With