1

I am trying to sort custom objects on some custom order but I am not able to. I can sort if objects are String or integer. I have posted some detailed description on code below. Thanks for any help.

Private static final List<String> places = Arrays.asList(“Switzerland”, “America”, “Romania”, “Chad”, "Australia"); 
//this list is fixed and always needs to maintain this order

Map<String, String> countrFromDB = countryDAO.getAllCOuntriesFromDB();

List<Country> sortCountry= new ArrayList<>();
for(Map.Entry<String, String> entry : countrFromDB.entrySet() ){
  Country c = new Country(entry.getKey(), entry.getValue());
  sortCountry.add(c);

  if(places.contains(countrFromDB.getKeyValue())){
    sortCountry.add(c.getKeyValue());
  }
}


for(Country data:sortCountry){
System.out.println(data.getKeyValue());
}

I get America, Chad, Australia, Switzerland, Romania. However, I need to maintain order like in

places = Switzerland, America, Romania, Chad, Australia
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user10806781
  • 41
  • 10
  • Post the country class along with the dataset which shows what is keyvalue against each country – Priyak Dey Mar 26 '20 at 11:54
  • "Nothing is working" is not a good enough problem description. Is it a compiler error? Behavior error? What does `getKeyValue` return? – M. Prokhorov Mar 26 '20 at 11:56
  • @M.Prokhorov it returns name of the country, I am getting output but it's not sorted. – user10806781 Mar 26 '20 at 12:00
  • @user10806781, then I can't reproduce your problem with the given code. For me it outputs country names in reverse lexical order, just like your `Comparator` says it should: `[Switzerland, Romania, Chad, Australia, America]`. Add a [mcve] to your question, with all necessary data so that running the code reproduces the problem. – M. Prokhorov Mar 26 '20 at 12:05
  • @M.Prokhorov I think my comparator is not sorting the way I want, thats the reason I posted this question. I don't want reverse lexical order, there is a fixed list in my code "places" - sorting should always maintain that order. Thanks for your effort – user10806781 Mar 26 '20 at 12:14
  • @user10806781, either way you need to fix your question. In one place you say it should sort by index in `places`. Then you show code that sorts in reverse-lexical order. Finally, at the very end you claim your output is not sorted at all, which is impossible with the code in your question's body. So, which one is it? – M. Prokhorov Mar 26 '20 at 12:19
  • @M.Prokhorov not the way I wanted it to work. – user10806781 Mar 26 '20 at 12:30
  • try my answer with `indexOf` inside a comparator – Dickens A S Mar 26 '20 at 12:33

2 Answers2

1

You have to use indexOf in the comparator

final List<String> places = Arrays.asList("Switzerland", "America", "Romania", "Chad", "Australia"); 
.....
.....
.....

Collections.sort(sortCountry, new Comparator<Country>(){
    public int compare(Country o1, Country o2){
        return places.indexOf(o1.getValue()) - places.indexOf(o2.getValue());
    }
});

for(Country data:sortCountry){
    System.out.println(data.getValue());
}
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
  • I am trying this, let's see how it goes – user10806781 Mar 26 '20 at 12:43
  • ultimately you need to play with `indexOf` try @Joe is answer, I don't to spend more time on this honestly :) :) – Dickens A S Mar 26 '20 at 12:57
  • thanks, honestly you need to tweak the code for -1 0 and +1 in the return statement, try it – Dickens A S Mar 26 '20 at 13:22
  • That comparator doesn't follow a contract. It will report any elements as being equally ordered if first argument is `Switzerland`, and it will only report that `o1` is less than `o2` if list doesn't contain `o1` at all. Surely you meant `Comparator.comparingInt(o1 -> places.indexOf(o1.getValue()))`? – M. Prokhorov Mar 26 '20 at 13:32
  • no I did not went thought any code, you are correct, places.indexOf will not tell `-1` `+1` and `0` , if comparingInt solves that then it should be a solution, even this code can be formalized with an offset formula which will return `-1` `+1` `0` honestly I did not want to spend more time :) – Dickens A S Mar 26 '20 at 13:51
  • Hi @M.Prokhorov, now this formula `o1.index - o2.index` will give accurate result exactly as per the fixed array order – Dickens A S Mar 27 '20 at 12:40
0

Why not construct the list in the order you want in the first place, so you don't need to sort it separately?

List<Country> sortCountry= new ArrayList<>();
for(String place : places){
  if (countrFromDB.containsKey(place)) {
    Country c = new Country(place, countrFromDB.get(place));
    sortCountry.add(c);
  }
}
Joni
  • 108,737
  • 14
  • 143
  • 193