1

Might be a silly question, but... From the docs, I know that in java I have this method called getFields() on the java.lang.Class<T> that will return an array of Field objects, and I also know that the results are not sorted in any way at all.

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.

Now what if I wanted to get those fields sorted? Is there a method out there that will return them for me? Or do I have to sort it myself?

From this question I know I can sort them using a Comparator. I just want to know if I really have to do this extra step myself.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • 1
    Standard JDK doesn't have any method which will do what you want so if you are limited to JDK only you need to do this extra step. For now I found something like http://stackoverflow.com/a/15075633/1393766 if you are interested in external libraries. – Pshemo Jan 15 '16 at 15:18
  • What general shape of solution would you be looking for, that gets them sorted without you having to sort them? I'm not trying to be flippant, but if they're not sorted, and you want them sorted... you need to sort them. That said, if you just don't want to write the Comparator, you may want to look into Guava's [Ordering.usingToString()](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Ordering.html#usingToString()). – yshavit Jan 15 '16 at 15:20
  • "if you just don't want to write the Comparator" Something like that haha. I was more curious than lazy. Also, nice solution from Guava. – Mauker Jan 15 '16 at 15:21
  • Okay, I know the question is silly, but is it really "downvote bad"? – Mauker Jan 15 '16 at 15:26
  • 3
    the extra step could look like `Arrays.sort(fields, comparing(Field::getName));` - does not look too bad, does it? – assylias Jan 15 '16 at 15:34
  • Oh. That does sounds good! And cleaner. Thanks! Write it as an answer, so I can accept it :) – Mauker Jan 15 '16 at 15:35
  • 1
    @assylias Should probably post an answer. – user1803551 Jan 15 '16 at 15:38

1 Answers1

2

Sorting an array of objects based on a property can be written in one line:

Field[] fields = String.class.getDeclaredFields();
Arrays.sort(fields, comparing(Field::getName));

using a static import: import static java.util.Comparator.comparing;.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • And as for the other comments, I know that I do have to sort it, and you showed me a very nice way of doing it. Thanks! – Mauker Jan 15 '16 at 15:44
  • 1
    Note that this solution requires Java 1.8 or later. If you're using an earlier release, it won't work: `java.util.Comparator.comparing` and `java.util.function.Function` were introduced in 1.8. (We still have some old servers running Java 1.6 where I work...) – Mike Harris Jan 15 '16 at 15:52