Programing
I have a List of Objects like List<Object> p.I want to sort this list alphabetically using Object name field. Object contains 10 field and name field is one of them.
List<Object> p
if (list.size() > 0) { Collections.sort(list, new Comparator<Campaign>() { @Override public int compare(final Object object1, final Object object2) { return String.compare(object1.getName(), object2.getName()); } } ); }
But there is nothing like String.compare..?
From your code, it looks like your Comparator is already parameterized with Campaign. This will only work with List<Campaign>. Also, the method you're looking for is compareTo.
Comparator
Campaign
List<Campaign>
compareTo
if (list.size() > 0) { Collections.sort(list, new Comparator<Campaign>() { @Override public int compare(final Campaign object1, final Campaign object2) { return object1.getName().compareTo(object2.getName()); } }); }
Or if you are using Java 1.8
list .stream() .sorted((object1, object2) -> object1.getName().compareTo(object2.getName()));
One final comment -- there's no point in checking the list size. Sort will work on an empty list.
The most correct way to sort alphabetically strings is to use Collator, because of internationalization. Some languages have different order due to few extra characters etc.
Collator
Collator collator = Collator.getInstance(Locale.US); if (!list.isEmpty()) { Collections.sort(list, new Comparator<Campaign>() { @Override public int compare(Campaign c1, Campaign c2) { //You should ensure that list doesn't contain null values! return collator.compare(c1.getName(), c2.getName()); } }); }
If you don't care about internationalization use string.compare(otherString).
string.compare(otherString)
if (!list.isEmpty()) { Collections.sort(list, new Comparator<Campaign>() { @Override public int compare(Campaign c1, Campaign c2) { //You should ensure that list doesn't contain null values! return c1.getName().compare(c2.getName()); } }); }
Have a look at Collections.sort() and the Comparator interface.
Collections.sort()
String comparison can be done with object1.getName().compareTo(object2.getName()) or object2.getName().compareTo(object1.getName()) (depending on the sort direction you desire).
object1.getName().compareTo(object2.getName())
object2.getName().compareTo(object1.getName())
If you want the sort to be case agnostic, do object1.getName().toUpperCase().compareTo(object2.getName().toUpperCase()).
object1.getName().toUpperCase().compareTo(object2.getName().toUpperCase())
public class ObjectComparator implements Comparator<Object> { public int compare(Object obj1, Object obj2) { return obj1.getName().compareTo(obj2.getName()); } }
Please replace Object with your class which contains name field
Usage:
ObjectComparator comparator = new ObjectComparator(); Collections.sort(list, comparator);
Using Java 8 Comparator.comparing:
list.sort(Comparator.comparing(Campaign::getName));
something like
List<FancyObject> theList = … ; Collections.sort (theList, new Comparator<FancyObject> () { int compare (final FancyObject a, final FancyObject d) { return (a.getName().compareTo(d.getName())); }});
Here is a version of Robert B's answer that works for List<T> and sorting by a specified String property of the object using Reflection and no 3rd party libraries
List<T>
/** * Sorts a List by the specified String property name of the object. * * @param list * @param propertyName */ public static <T> void sortList(List<T> list, final String propertyName) { if (list.size() > 0) { Collections.sort(list, new Comparator<T>() { @Override public int compare(final T object1, final T object2) { String property1 = (String)ReflectionUtils.getSpecifiedFieldValue (propertyName, object1); String property2 = (String)ReflectionUtils.getSpecifiedFieldValue (propertyName, object2); return property1.compareToIgnoreCase (property2); } }); } } public static Object getSpecifiedFieldValue (String property, Object obj) { Object result = null; try { Class<?> objectClass = obj.getClass(); Field objectField = getDeclaredField(property, objectClass); if (objectField!=null) { objectField.setAccessible(true); result = objectField.get(obj); } } catch (Exception e) { } return result; } public static Field getDeclaredField(String fieldName, Class<?> type) { Field result = null; try { result = type.getDeclaredField(fieldName); } catch (Exception e) { } if (result == null) { Class<?> superclass = type.getSuperclass(); if (superclass != null && !superclass.getName().equals("java.lang.Object")) { return getDeclaredField(fieldName, type.getSuperclass()); } } return result; }
You can use sortThisBy() from Eclipse Collections:
sortThisBy()
MutableList<Campaign> list = Lists.mutable.empty(); list.sortThisBy(Campaign::getName);
If you can't change the type of list from List:
List
List<Campaign> list = new ArrayList<>(); ListAdapter.adapt(list).sortThisBy(Campaign::getName);
Note: I am a contributor to Eclipse Collections.
If your objects has some common ancestor [let it be T] you should use List<T> instead of List<Object>, and implement a Comparator for this T, using the name field.
T
List<Object>
If you don't have a common ancestor, you can implement a Comperator, and use reflection to extract the name, Note that it is unsafe, unsuggested, and suffers from bad performance to use reflection, but it allows you to access a field name without knowing anything about the actual type of the object [besides the fact that it has a field with the relevant name]
In both cases, you should use Collections.sort() to sort.
Try this:
List< Object> myList = x.getName; myList.sort(Comparator.comparing(Object::getName));
This is assuming a list of YourClass instead of Object, as explained by amit.
YourClass
Object
You can use this bit from the Google Guava library:
Collections.sort(list, Ordering.natural() .onResultOf(new Function<String,YourClass>() { public String call(YourClass o) { return o.getName(); })) .nullsLast();
The other answers which mention Comparator are not incorrect, since Ordering implements Comparator. This solution is, in my opinion, a little easier, though it may be harder if you're a beginner and not used to using libraries and/or "functional programming".
Ordering
Copied shamelessly from this answer on my own question.
Using a selection Sort
for(int i = list.size() - 1; i > 0; i--){ int max = i for(int j = 0; j < i; j++){ if(list.get(j).getName().compareTo(list.get(j).getName()) > 0){ max= j; } } //make the swap Object temp = list.get(i); list.get(i) = list.get(max); list.get(max) = temp; }
If you are using a List<Object> to hold objects of a subtype that has a name field (lets call the subtype NamedObject), you'll need to downcast the list elements in order to access the name. You have 3 options, the best of which is the first:
NamedObject
List<NamedObject>
Option 3 would look like this:
Collections.sort(p, new Comparator<Object> () { int compare (final Object a, final Object b) { return ((NamedObject) a).getName().compareTo((NamedObject b).getName()); } }
if(listAxu.size() > 0){ Collections.sort(listAxu, new Comparator<Situacao>(){ @Override public int compare(Situacao lhs, Situacao rhs) { return lhs.getDescricao().compareTo(rhs.getDescricao()); } }); }
참고URL : https://stackoverflow.com/questions/8432581/how-to-sort-a-listobject-alphabetically-using-object-name-field