previous_developer
previous_developer

Reputation: 10988

Sorting LinkedList with specific variable of object?

I have a bubble sort function that orders LinkedList of SomeObj by "a" variable. What if i want to order the list by "b" variable at the same time? What can i do instead of writing another function with b?

public static void BubbleSort(LinkedList<SomeObj> objs) {
    int len = objs.size();

    for(int pass = 1; pass < len; pass++) {
        for (int i=0; i < len - pass; i++) {
            if(objs.get(i).a > objs.get(i + 1).a) {
                SomeObj p = objs.get(i);
                objs.set(i,objs.get(i+1));
                objs.set(i + 1, p);
            }
        }
    }
}

Upvotes: 0

Views: 1116

Answers (7)

Pshemo
Pshemo

Reputation: 124225

For sorting List you can use

Collections.sort(List list, Comparator c));

like in this main method

class Pair{
    int a;
    int b;

    public Pair(int a, int b) {
        this.a=a;
        this.b=b;
    }

    public String toString() {
        // TODO Auto-generated method stub
        return "["+a+","+b+"]";
    }

    //test
    public static void main(String[] args) {
        Comparator<Pair> comparatorA=new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                if (o1.a>o2.a) return 1;
                if (o1.a<o2.a) return -1;
                return 0;
            }
        };
        LinkedList<Pair> list=new LinkedList<>();
        list.add(new Pair(1,2));
        list.add(new Pair(2,1));
        list.add(new Pair(3,1));
        list.add(new Pair(1,3));

        Collections.sort(list, comparatorA);
        System.out.println(list);
    }
}

Now you can just make comparator for b value and use Collections.sort with that comparator

Upvotes: 1

Oleg
Oleg

Reputation: 615

I guess there is a kind of misunderstanding - it's not about sorting by a and then by b, it's a kind of 2 lists in one, and user wants to sort them both independently but without creating the second list, or am I not right?

Upvotes: 1

Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

Implement Comparator interface in a class with its compare method. that accepts two objects and compares them which returns -ve,0,+ve values to tell less than , equal or greater than.

create an object of this Comparator and pass it to bubble sort method and let it use its compare method to compare the two object.

Your object should have getters for all such fields.

Also whenever you wanna change the object's criteria for comparison , use a different Comparator.

Check this example.

Upvotes: 4

Oleg
Oleg

Reputation: 615

I think the simplest way to do that is swap variable values rather than list items. Using this approach you can "sort" the list in different ways simultaneously.

Upvotes: 1

Martin
Martin

Reputation: 1140

If you can a good way to sort by first "a" then "b" is to implement Comparable in your SomeObj class and make a compareTo method that compares on both a and b. http://www.javapractices.com/topic/TopicAction.do?Id=10

Upvotes: 2

John3136
John3136

Reputation: 29265

Decide which has the highest priority. Check the higher priority variable - if they are different, just sort as if you are only sorting on that. If the high priority variable matches, then you have to go back to the lower priority one, and base your whole compare on that.

if (obj1.getA() != obj2.getA()) {
    // do compare on As
    // e.g. return onj2.getA() - obj1.getA()
}
else { // A's match, so do compares on B
    // e.g. return obj2.getB() - obj1.getB()
}

Upvotes: 1

Francis Upton IV
Francis Upton IV

Reputation: 19443

You would have your SomeObj implement the Comparable interface and then have your sort interact with it using that interface.

Upvotes: 2

Related Questions