javaalgorithmlogic

Sort Java object and find relative position based on a attribute


I have an interesting problem.
Here is the object structure:

public class Testdata {
    //Which is a consecutive running number i.e 1,2,3..etc
    private int sequence;
    
    //classified based on this again any random numbers
    private int window; 
    
    //need to calculate
    private int windowposition; 
    
}

Now, based on the sequence and window, I would need to derive windowposition in relation to the window

Test Data:
So, for testdata sequence/window

        1 / 2
        2 / 3
        3 / 2
        4 / 3
        5 / 3

Expected Output:

    sequence/window :   window position would be (in the same order)
    
    1 / 2       :   1
    
    2 / 3       :   1
    
    3 / 2       :   2
    
    4 / 3       :   2
    
    5 / 3       :   3

Update:

Yes indeed, I have already implemented Comparable and sorted the list to below order

1 / 2
3 / 2
2 / 3        
4 / 3
5 / 3

Now, how do I calculate the windowposition of each element in relation to its window?


Solution

  • It would probably make sense to implement Comparable. That allows your objects to be sorted. You would implement compareTo(T) like so:

    int compareTo(Testdata o) {
      return ((Integer)this.sequence).compareTo(o.sequence);
    }
    

    That way your objects can be sorted by sequence.

    Now collect all the objects with window 1 into a List, objects with window 2 into another list etc.

    HashMap<Integer, ArrayList<Testdata>> map = new HashMap<Integer, ArrayList<Testdata>>();
    
    // Add all the objects like this
    while (...) { // While there are more objects
      Testdata td = ... // Get next object
    
      List<TestData> list = map.get(td.window);
      if (list == null) {
        list = new ArrayList<Testdata>();
        map.put(td.window, list);
      }
    
      list.add(td.sequence);
    }
    

    Sort all of the lists using Collections.sort(List):

    for (ArrayList<TestData> list : map) {
      Collections.sort(list);
    }
    

    You then have one list for each window, accessible via map.get(window). Each of these lists has the object with the lowest sequence as its first object, second lowest as second object etc. -> the window position is the index of the object + 1.

    EDIT:

    If your objects are already sorted by window and sequence (into a single list), you can do something like this to assign the window positions:

    int window = 1;
    int wp = 0;
    for (Testdata td : list) {
      if (td.window > window) {
        wp = 1;
        window = td.window;
      } else {
        wp++;
      }
    
      td.windowposition = wp;
    }