0

I would like to sort internal List<Cat> with it id property, where Cat.class looks like following.

public class Cat {

    private String id; //e.g. havs, jf22, 1999, jsav, 9999
    private String name;
    private String race;

}

And this cat list object is part of other class Zoo

public class Zoo {

    private List<Cat> cats;

}

As an example id can have multiple forms, lets say for exmple purposes I will only specify 4 of them:

[zavs, jf22, 1999, asav, 9999, zzdo331], so I am having 6 different cats with those ids. And I would like to sort them with following conditions:

  1. sort as text everything what is not number,
  2. ids which can be casted to integers/long will be sorded as numbers

so end result would looks like following: [asav, jf22, zavs, zzdo331, 1999, 9999]

so I was trying to do obvious stuff:

zoo.getCats.sort(Comparator::compring(Cat::getId))

but obviously it will treat id as a string, and will sort 1000 before 150.

Any ideas? I think PatterMatcher would be best and most optimal, but I have no idea how to start

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Suule
  • 2,197
  • 4
  • 16
  • 42

0 Answers0