Forum Stats

  • 3,853,503 Users
  • 2,264,229 Discussions
  • 7,905,374 Comments

Discussions

Sort List<Object> values without using Collections(not using compare)

ss_itc
ss_itc Member Posts: 16
edited Nov 21, 2018 1:11PM in Java Programming

Hi all,

I have a class type of Object and inserting these values into List.

Eg:  Employee class is having

int id, String name, String mail,

Employee e= new Employee(3,"Shankar","[email protected]");

Employee e1= new Employee(2,"Sekahat","[email protected]");

Employee e2= new Employee(37,"Sharat","[email protected]");

Employee e3= new Employee(1,"swaroop","[email protected]");

List<Employee> empList = new ArrayList<Employee>();

empList.add(e);

empList.add(e1);

empList.add(e2);

empList.add(e3);

Now I want to sort the list values based on id declared in the Employee class like below without using Collections.

1,"swaroop","[email protected]"

2,"Sekahat","[email protected]"

3,"Shankar","[email protected]"

37,"Sharat","[email protected]"

Can anyone please help on how to do this.

Thanks & Regards,

Suresh

Answers

  • Unknown
    edited Nov 21, 2018 1:11PM
    Now I want to sort the list values based on id declared in the Employee class like below without using Collections.

    Why? Seems silly not to use functionality that already exists.

    Can anyone please help on how to do this.

    Sure - write your own sort function.

    Just search the web for sort routines and use one of the MANY algorithms (bubble, etc) you find.

    Sounds like a class assignment to me. Because other than trying to understand/code/debug how a sort works it is a HUGE waste of time and effort to duplication functionality that is already available for free.