0

I was handed this assignment:

Complete the function void sort(List l) that sorts a list l that contains the strings "one","two","three","four" (although not necessarily in that order). You should declare a Comparator, and use this with the Collections.sort function. The comparator should use the comp function described above. The list l will be altered when it is sorted.

and the code I have already writen is this:

import java.util.*;

public class CW3 {

    private static HashMap < String, Integer > map = new HashMap < String, Integer > ();

    public CW3() {
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);

        List listB = Arrays.asList("A", "B", "C");
    }

    public static void main(String[] args) {
        System.out.println(new CW3().comp("one", "two"));
        System.out.println(new CW3().comp("two", "one"));
        System.out.println(new CW3().comp("one", "one"));
    }

    int comp(String s1, String s2) {
        int i1 = map.get(s1);
        int i2 = map.get(s2);
        return (i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
    }

    void sort(List l) {
        Comparator c = new Comparator() {

            public int compare(Object o1, Object o2) {
                return 0; // FIXME change this so it calls comp
            }
        };
        // now sort l using the comparator c
        // FIXME complete this line
    }

Any ideas where to start? it says list so I would have to create a list but then how would I sort them?

4
  • 1
    In which order should the sorted list be? The task does not define that. Commented Mar 11, 2014 at 17:50
  • That is exaccctttlyyy what I am confused with.. Commented Mar 11, 2014 at 17:51
  • One issue is that you're not explicitly declaring a comparator. You're just writing a method that does the compare logic Commented Mar 11, 2014 at 17:51
  • comp is already defined. All you need is to call it. This is a very simple assignment: just follow FIXME instructions. Commented Mar 11, 2014 at 17:56

2 Answers 2

1

What you need to do is to define the compare method. It should take two objects o1 and o2 as parameters and return

  • -1 when o1 < o2
  • 0 when o1 == o2
  • 1 when o1 > o2

Your Comparator uses this method as a basis to decide the order of the elements. Then you sort the list l by calling Collections.sort(l, c), where c is the Comparator you have defined.

Sign up to request clarification or add additional context in comments.

2 Comments

Where would the objects come from? its pretty simple but im so confused
It doesn't matter where do they come from. You just define the compare method and Comparator knows how to use it. Similarly, when you have defined the Comparator, you call the sort method and it already implements a sorting algorithm, you only need to supply the Comparator.
0

you can use below compar method

public class StepComparator implements Comparator<Step>{

@Override
public int compare(String so1, String s2) {

    if(map.get(s1)>map.get(s2)) return 1;

    return -1;
}

}

then you use this to sort them

StepComparator  comparator = new  StepComparator();
Collections.sort(list,comparator);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.