Alphabetically Sort an Arraylist

Asked

Viewed 1,585 times

1

I have an Arraylist called Test with the fields:

public class Teste 
{
    private String dctitle;
    private String rdfabout;
    private String dbid;
    private String depto;
    private String sigla;
    private String ciclo;
    private String state;
    private String dataplanejadainicial;
    private String dataEntradaEstado;
    private String id;
    private String executante;
}

This Arraylist has N items and I need to create a "sort" method that will receive as parameter the ArrayList<Teste> teste and return me an Arraylist with Testing ordered by the Departments.
I know you have a method called .Sort but I don’t know how to use.


NOTE: in the Test class are implemented all the Get and Set attributes, just not put to not make the question too big

1 answer

1


Use the Collections.Sort with the Interface Comparator to order the same array, minimal example:

Teste a = new Teste();
a.setDepto("3");
Teste b = new Teste();
b.setDepto("2");
Teste c = new Teste();
c.setDepto("1");

ArrayList<Teste> testes = new ArrayList<>();
testes.add(a);
testes.add(b);
testes.add(c);


Collections.sort(testes, new Comparator(){
    @Override
    public int compare(Object o1, Object o2) {
        Teste a1 = (Teste)o1;
        Teste a2 = (Teste)o2;
        return a1.getDepto()
                .compareToIgnoreCase(a2.getDepto());
    }

});

or

Collections.sort(testes, new Comparator<Teste>(){
    @Override
    public int compare(Teste o1, Teste o2) {                
        return o1.getDepto()
                .compareToIgnoreCase(o2.getDepto());
    }
});

or

If you want to order even accented words:

Collections.sort(testes, new Comparator<Teste>(){
    private Collator collator = Collator.getInstance(Locale.US);            
    @Override
    public int compare(Teste o1, Teste o2) {    
        return getCollator().compare(o1.getDepto(), o2.getDepto());                
    }

    public Collator getCollator() {
        collator.setStrength(Collator.PRIMARY);
        return collator;
    }

});

ONLINE EXAMPLE Ideone

References:

  • so I’m trying to understand how this . Sort it returns me a new array already in order, from the first item to the last?

  • This @Mariteixeira he will catch that array and order for you all the elements contained, in case I made the comparison on getDepto() which would be the department!. Actually it’s the same ordered array.

  • @Mariteixeira was clear the answer?

  • was... I’m just getting mixed up in the use. I thought of creating a "new array" already sorted pq I will have to use this array in other methods as well. Like this function .sort does not return a new one, it changes the array being passed as parameter, I have to do this method Collections.sortevery time I need it ordered?

  • @Mariteixeira will be used only when it is not ordered and of course you need to order it, example change in the array, how to increase the array as new items...

  • I’ll have to use it at all times ordained

  • 1

    I did it! Thank you, I was confused but now I got it!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.