0

I have a list of objects i created that store money values, kind of like accounts at a bank. I have a loop that updates to sort the objects every time one of the values is changed using the compare To method i made for them. Is there anyway I can sort these without checking each object in the loop( decrease the amount of loops that occur or get rid of it entirely).

Thank you so much guys i'm relatively new at sorting and efficiency so please go easy on me :)

Edit: there not simply integers their integers stored in objects that are getting retrieved through the loop

  • If the number of updates is bigger than the number of times you need to get the sorted list, just don't store the list when updating it, but only sort it when getting it. That said, before searching for a better solution than the one you have, have you measured it, and found that it was too slow, and that this sort was the culprit? Because otherwise, you're just preoptimizing, and that is the root of all evil. – JB Nizet Nov 07 '16 at 19:18
  • A suggestion with some caveats: Use a sorted set https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html . When changing a value, remove from set, change, and re-add. The caveats: consider JB Nizet's point above, and read the javadoc about relationship between sort order and `equals`. – Taylor Nov 07 '16 at 19:30
  • Are you asking what the most efficient way is, or are you asking how to sort, in the easiest, natural way. If the latter, a google search will return tons of examples. But it boils down to `list.sort(Comparator.comparingInt(MyObject::getMoneyValue))` – JB Nizet Nov 07 '16 at 19:37
  • I'm asking for most efficient way JB. Thanks for feed back though guys! This has all been very helpful :D –  Nov 07 '16 at 20:20
  • See also http://stackoverflow.com/questions/5393254/java-comparator-class-to-sort-arrays and http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property (lists but very similar) – Tunaki Nov 07 '16 at 22:22

1 Answers1

0

I think that Java have efficient methods for ordering, isn't neccesary to create ordering methods.

If you have method compareTo, you can do a Java collection (for example a list) and use the methods that comes with it.

For example:

import java.util.List;
import java.util.ArrayList;

public class Employee implements Comparable<Employee>{

    private int id;
    private String name;
    private String surname;
    private float salary;

    public Employee(int id, String name, String surname, float salary) {
        super();
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.salary = salary;
    }

    //  Compare 2 employees by salary
    @Override
    public int compareTo(Employee employee) {      
        //  Compared by salary
        if (this.salary < employee.salary) {    
            return -1;
        }
        else if(this.salary > employee.salary){
            return 1;
        }

        return 0;
    }   

    @Override
    public String toString() {
        return "Employee [name=" + name + ", salary=" + salary + "]";
    }

    public static void main(String args[]){
        Employee employee1 = new Employee(1, "One", "One Surname", 1800);
        Employee employee2 = new Employee(2, "Two", "Two Surname", 200);
        Employee employee3 = new Employee(3, "Three", "Three Surname", 1500);
        Employee employee4 = new Employee(4, "Four", "Four Surname", 800);

        List<Employee> list = new ArrayList<>();
        list.add(employee1);
        list.add(employee2);
        list.add(employee3);
        list.add(employee4);

        System.out.println(list);   //  Unordered list

        list.sort(null);

        System.out.println(list);   //  Ordered list by salary

    }
}

You will have this output:

[Employee [name=One, salary=1800.0], Employee [name=Two, salary=200.0], Employee [name=Three, salary=1500.0], Employee [name=Four, salary=800.0]]

[Employee [name=Two, salary=200.0], Employee [name=Four, salary=800.0], Employee [name=Three, salary=1500.0], Employee [name=One, salary=1800.0]]

Dani
  • 1,825
  • 2
  • 15
  • 29