1

I have a TreeTable and would like to perform the sort by number and alphabetically when clicking on header.

Example:

  • On a first click, I have to check that the column content is sorted by number
  • If I click on another column that contains String data, I have to check that column content is sorted alphabetically.

Are there known functions that could I use?

I've used Collections for sorting number , but how do I can make the sort alphabetically ? Collections.sor(myList) is OK for sorting by number but I would sort data alphabetically.

thanks

1 Answer 1

1

This can easily be done via Collections.sort(...). Create a copy of your list, sort it and check if they are equal.

Example:

List <String> copy = new ArrayList <String>(original);
Collections.sort(copy);
assertEquals(copy, original);

This can be done, if the elements in the list are comparable (i.e. are of type T implements Comparable <T>). Strings are comparable, and their default comparator sorts them alphabetically (though upper-case are always comes before lower-case)

You may also provide a Comparator for a more flexible sorting.

Here is a more complicated example.

List <String> unholyBible = new ArrayList <String>();
unholyBible.add("armageddon");
unholyBible.add("abyss");
unholyBible.add("Abaddon");
unholyBible.add("Antichrist");
Collections.sort(unholyBible);
System.out.println(unholyBible);

This will print us [Abaddon, Antichrist, abyss, armageddon]. This is because default comparation is case-sensitive. Lets fix it:

List <String> unholyBible = new ArrayList <String>();
unholyBible.add("armageddon");
unholyBible.add("abyss");
unholyBible.add("Abaddon");
unholyBible.add("Antichrist");
Collections.sort(unholyBible, new Comparator <String>() {
    public int compare(String o1, String o2){
        return o1.compareToIgnoreCase(o2);
    }
});
System.out.println(unholyBible);

This one prints [Abaddon, abyss, Antichrist, armageddon].

Now you may worship Satan in strict alphabetical order.

See also

Sign up to request clarification or add additional context in comments.

13 Comments

Frozen: I'm wrinting Junit test to verify the sort on table : getValue on column returns mean ArrayList od data and I would like to check that data is sorted alphabeticcaly
Sorry, your "first click" reference has confused me. Corrected the answer.
Frozen, it works when the content is a number but in the case where my conlumn content has : "abc",efd""abg",.. it doesn't make the sort alphabetically, this is my problem
could you develop more on using comparator, I search also in google but I don't find some precisions about sort alphabatically on ArrayList
@lola Collections.sort() dose support string sorting, because String has its own compareTo() implementment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.