1

I have an object array as below.

[obj1 obj2 obj3 ...]

Each object in the array is having properties date (java.util.Date) and name (String).

obj1.date = new Date(); obj1.name = "A";

The objects in the array have already been sorted based on the date. More than one object can have same date values.

Now, I want to sort the array of objects based on name of object if objects are having same date. To be more clear, there should not be any change in the order of objects if dates of all objects are different. If objects are having same date, sort only those objects based on name. Note that the current array is already sorted based on date.

For an example I have the below array [obj1,obj2,obj3,obj4]

obj1.name = "B"; obj1.date = Tue Jul 01 00:00:00 IST 2014;

obj2.name = "D"; obj2.date = Thu Jul 03 00:00:00 IST 2014;

obj3.name = "A"; obj3.date = Thu Jul 03 00:00:00 IST 2014;

obj4.name = "C"; obj4.date = Sun Jul 06 00:00:00 IST 2014;

After sorting array should be [obj1,obj3,obj2,obj4]

3 Answers 3

2

Smutje gave you the answer how to do this in Java 5 to 7. With Java8 you can make use of the functional interfaces and lambda expressions. So your sorting method would reduce to

Arrays.sort(objectArray, Comparator.<YourObject>comparing(YourObject::getDate)
        .thenComparing(YourObject::getString));
Sign up to request clarification or add additional context in comments.

1 Comment

As I am steadily improving my knowledge of Java 8, thank you very much for this concise solution!
1

Write a Comparator<YourObject> which first compares dates and, if the dates are equal, the names and sort the array

Example

YourObject

public class YourObject {

    private Date date;

    private String string;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }
}

Sorting

Arrays.sort(new YourObject[]{}, new Comparator<YourObject>() {

        @Override
        public int compare(YourObject o1, YourObject o2) {
            final int dateComparisonResult = o1.getDate().compareTo(o2.getDate());
            final int yourObjectComparisonResult;

            if (dateComparisonResult == 0) {
                yourObjectComparisonResult = o1.getString().compareTo(o2.getString());
            } else {
                yourObjectComparisonResult = dateComparisonResult;
            }

            return yourObjectComparisonResult;
        }
    });

Comments

1
Object[] obj = {obj1,obj2,obj3};

your array like this right

you just convert this into `java.util.List` , because it's having helpful API(classes & interfaces), so that you can easily access that API instead of your own tough coding

Step-1: List l = Arrays.asList(obj);


once you get the reference of List you can sort out the list by using `Collections.sort(Collection)`

Step-2: like, Collections.sort(l);


if you want your own sort technique like, By date or By name you just simply use java.util.Comparator interface

for example, Byname basis sorting the code snippet like this,


class ByName implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {
        // TODO Auto-generated method stub
        if(str1.length > str2.length)
           return 1;
        else
           return -1;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.