1

Im a Java noob but still trying to learn by reverse engineering codes. I read similar questions but still not able to figure out.

Ive defined a quadruplet array pair as :-

The content of pairList array is (1:2:3:6), (3:4:1:1), (1:2:3:3)

I want to ascending sort the pairList based on the fourth element, so the output should be (3:4:1:1), (1:2:3:3), (1:2:3:6)

public class Pair<L,C, R, S> {
        private L l;
        private R r;
        private C c;
        private S s;
        public Pair(L l, R r, C c, S s){

            this.l = l;
            this.r = r;
            this.c = c;
            this.s = s;
        }
        public L getL(){ return l; }
        public R getR(){ return r; }
        public C getC(){ return c; }
        public S getS(){ return s; }

        public void setL(L l){ this.l = l; }
        public void setR(R r){ this.r = r; }
        public void setC(C c){ this.c = c; }
        public void setS(S s){ this.s = s; }


    }

 List<Pair<String,Integer,Integer, Integer>> pairList = new ArrayList<Pair<String,Integer,Integer, Integer>>();
 // Code for array population

 Arrays.sort(pairList, new Comparator<Pair>() {
        @Override
        public int compare(Pair p1, Pair p2) {
            return p1.getL().compareTo(p2.getL());
        }
    });

But Im getting this error :-

 error: cannot find symbol
                return p1.getL().compareTo(p2.getL());
                                ^
  symbol:   method compareTo(Object)
  location: class Object
  • 4
    Your problem is that you are using `Pair` as a raw type in your comparator, meaning you lose all information of what those pairs contain. The solution is to not use raw types and instead the correct generic parameters so that java knows what kind of objects your Pair contains. See: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – OH GOD SPIDERS Aug 25 '21 at 09:38
  • I did this but even this isnt working --> public class Pair implements Comparable Pair . Sorry but I cant comprehend all the information in link you shared at this stage of learning. Can you please help me with my code Sir. – Ankita Jain Aug 25 '21 at 09:45
  • Try to use ``public class Pair, C, R, S>`` and ``Collections.sort`` instead of ``Arrays.sort`` (or ``Comparator> comparator = Comparator.comparing( Pair::getL ); pairList.sort( comparator );``). – MichaelCkr Aug 25 '21 at 09:47
  • 1
    @AnkitaJain Another error is you are trying to use `Arrays.sort` to sort an ArrayList. You can only sort arrays with that method not lists. Just use the lists sort method and as already said make sure you use the same generic type arguments for you pair everywhere: `pairList.sort(new Comparator>() { public int compare(Pair p1, Pair p2) { //code here } }` – OH GOD SPIDERS Aug 25 '21 at 09:53
  • 1
    @MichaelCkr :-) Thanks Michael exactly what I was looking for. public class Pair worked for me – Ankita Jain Aug 25 '21 at 09:54
  • @OHGODSPIDERS Ok sir , Ill try this also – Ankita Jain Aug 25 '21 at 09:55

0 Answers0