java - Sort list of methods -
this question has answer here:
- sort arraylist of custom objects property 23 answers
i have list contains setters of pojo class.
public static void main(string[] args) throws exception { method[] publicmethods = sampleclass.class.getmethods(); list<method> setters = new arraylist<>(); (method method : publicmethods){ if (method.getname().startswith("set") && method.getparametercount() == 1) { setters.add(method); } } } in documentation order of method list not guaranteed. question how can sort list of setters alphabetically?
you need custom comparator:
collections.sort(setters, new comparator<method> { @override public int compare(method a, method b) { return a.getname().compareto(b.getname()); } });
Comments
Post a Comment