1

I have code:

    List<ModelMap> nameCustomer = new ArrayList<ModelMap>();

    for (int i=1;i<150;i++) {
                    ModelMap map = new ModelMap();
                    map.put("name", "nazwa " + i);
                    map.put("surname", "nazwisko " + i);
                    map.put("number", "123 ");
                    nameCustomer.add(map);
                }

            ModelMap[] result = new ModelMap[nameCustomer.size()];
            ModelMap[] rowsArray = nameCustomer.toArray(result);

How can I sort nameCustomer (or rowsArray) by surname or number?

faszynski
  • 419
  • 3
  • 11
  • 26
  • Look at `Arrays.sort()` for the array and `Collections.sort()` for the list. – jlordo Dec 03 '12 at 13:05
  • possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – Thilo Dec 03 '12 at 13:05

5 Answers5

1

You need to implement a Comparator that defines your sorting criteria,
then you can use Arrays.sort for arrays, or Collections.sort for Lists, or dump everything into a SortedSet (e.g., TreeSet)

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
1

You can use the Collections.sort(nameCustomer, comparator) method to sort your list. For that you have to write the code for your Comparator.The code for Comparator will be -

public Comparator<ModelMap> getComparator(final String sortBy){
   if ("surname".equals(sortBy)) {
   return new Comparator<ModelMap>() {
   @Override int compare(ModelMap m1, ModelMap m2) 
    return m1.getSurname().compareTo(m2.getSurname());
   }};} 
     else if (condition) {// ...  }
      else {
        throw new IllegalArgumentException("invalid sort field '" + sortBy + "'"); }
       }

I am assuming that you have a getSurname method written in your ModelMap class to get the surname. If not then write it to make the above code work.

You can visit here for further help. Cheers !!

Community
  • 1
  • 1
Alim Ul Gias
  • 6,351
  • 2
  • 28
  • 39
0

You'll probably want to use the Collections.Sort package. Lists maintain "natural ordering" to maintain consistency. e.g.

    List<Name> names = Arrays.asList(nameArray);
    Collections.sort(names);
    System.out.println(names);
Jeff Watkins
  • 6,343
  • 16
  • 19
  • But how to do that if you have a two-level List/Map or similar structure ? The only solution I see is to implement a custom iterator to return a specified sublevel-key, then extract it as an array, then finally sort it. – Benj Dec 03 '12 at 13:16
  • 1
    As the other answers state, you should use a custom Comparator. – Jeff Watkins Dec 03 '12 at 13:29
  • So this list does not answer the question because it tells OP that `Collections.sort()` does use natural ordering, which is not what he wants... – brimborium Dec 03 '12 at 13:30
  • Yes, I read the doc one more time, and effectively it is easily possible to implement such a thing, and smarter to do it this way. – Benj Dec 03 '12 at 13:32
0

using the Colections.sort() and give it a custom Comparator class implementation, which will do a string compare by surname.

Or use a LinkedHashMap, which has consistency after put() ang get() and keys sorted.

I hope it helps!

  • How should be write my Comparator? – faszynski Dec 03 '12 at 13:25
  • Exactly what I wrote in profile: the fish needed ( the complete source coe ready to copy-paste) not the fishing knowledge...Not even an upvote for accepted answer, what a shame! –  Dec 03 '12 at 17:08
0

You can implement your own iterator on List, which could be parametrized to return only the inner key you specified.

Then, "iterating" with a standard sorter around it does the trick.

Benj
  • 1,184
  • 7
  • 26
  • 57