1

I have

class cluster{
    string name;
    string id;
    list<Feature> flist;
}

class Feature{
    string name;
    string id;
}

cluster can have multiple lists of Feature

Now I have list of Feature say list<Feature> list={f1,f2,f3,f4} , thease Feature objects can be spread across different cluster objects and some might be not part of any cluster.

I have list of cluster and I have to sort based on the list of Feature I have.

input:
cluster1-f2,f6,f7
cluster2-f3,f4,f5
cluster3-f9,f8
cluster4-f1,f10
cluster5-f11,f12



 output:
 cluster4-f1,f10
 cluster1-f2,f6,f7
 cluster2-f3,f4,f5
 cluster3-f9,f8
 cluster5-f11,f12

Please help me to sort cluster objects based on the given Feature list.

1
  • 3
    This isn't a coding-writing service, what have you tried? Commented Feb 10, 2016 at 4:17

1 Answer 1

1

I'm not a Java expert, but below is how I sort custom object arrays. In my example, I have an array of Card objects and can call sortByInterval to sort the array (without returning a new array). In the Comparator class you return either -1, 1 or 0 so you can add whatever code you need to compare.

public static void sortByInterval (ArrayList<Card> array) {
    Collections.sort(array, new SortByIntervalComparator());
}

private static class SortByIntervalComparator implements Comparator<Card> {
    public int compare(Card card1, Card card2) {

        if (...) return -1      // -1: obj1 first
        else if (...) return 1; // 1: obj2 first
        else return 0;      // equal sort
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

using comperator is the proper way to set the order!

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.