roshanK
roshanK

Reputation: 409

Ordering of objects in java

is there any way to sort the objects in java. I dont want to implement comparable interface or any other sorting algorithms.

Upvotes: 0

Views: 461

Answers (5)

Stephen C
Stephen C

Reputation: 718768

You cannot sort things without (at least) using a sorting algorithm.

You also can't sort arbitrary objects, since Java provides no reliable ordering that will work for arbitrary objects. (See below.)

However, if your objects are instances of classes that support Comparable ... or you can implement a Comparator, then you can sort them, using:

   Arrays.sort<T>(T[], Comparator<T>) 

or

   Arrays.sort<T extends Comparable>(T[]) 

In short, you have to implement Comparable or provide a Comparator. But if you do, the Java class library provides a good general-purpose sort algorithm that you can use.


The two obvious approaches for comparing incomparable objects won't work:

  • You could compare the results of their toString() methods. However, there's no guarantee that

    a.toString().equals(b.toString())  IMPLIES  a.equals(b)
    
  • You could compare their hashCodes, or their identity hashCodes, but hashcode equality does not imply object equality either.

It is theoretically possible to use reflection to access all of an object's fields, extract their values and compare them in some predictable sequence to give an ordering. This might work, but you'd end up with an ordering that was expensive to compute, that didn't make much sense, and was (if you weren't careful) inconsistent with equals(Object). So this sounds like a bad idea.

Upvotes: 3

Babar
Babar

Reputation: 2826

Code sample from java tutorials. This sample code doesn't implement sorting algorithm, what it is doing is that it is telling the Sort algorithm which field to sort the objects on. Since there can be many fields in an object this part is required.

import java.util.*;
public class EmpSort {
    static final Comparator<Employee> SENIORITY_ORDER =
                                 new Comparator<Employee>() {
        public int compare(Employee e1, Employee e2) {
            return e2.hireDate().compareTo(e1.hireDate());
        }
    };

    // Employee database
    static final Collection<Employee> employees = ... ;

    public static void main(String[] args) {
        List<Employee>e = new ArrayList<Employee>(employees);
        Collections.sort(e, SENIORITY_ORDER);
        System.out.println(e);
    }
}

EDIT

The code sample here shows how same object collection sorted in different orders based on different fields.

Upvotes: 1

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

Alternatively, ensure correct position when insertion, something like balanced-tree. Comparing is inevitable.

Upvotes: 0

janhink
janhink

Reputation: 5023

Sorting a collection of objects which don't themselves implement comparable can be achieved like this (assuming MyObject is the object you want to compare and sort):

List<MyObject> myList = new ArrayList<MyObject>();

// add MyObject instances to the list

Collections.sort(myList, new Comparator<MyObject>()
    {

        @Override
        public int compare(MyObject o1, MyObject o2)
        {
            // perform your custom comparison of the two objects
            // and return the result
        }
    })

But without any Comparator or Comparable implementation there's obviously no way you could sort a collection of objects.

Upvotes: 3

Ingo
Ingo

Reputation: 36339

Sort objects without a sorting algorithm? Hardly. In no language. What I am missing in your question is: "I want to avoid strict ordering criteria."

Upvotes: 1

Related Questions