#Comparator, compare help

46 messages · Page 1 of 1 (latest)

flat drum
#

In Comparator, the arguments of Compare() are supposed to be swapped when 1 is returned, but i am seeing otherwise, what am I doing wrong? and also what is the order of the arguments?

import java.util.Comparator;
import java.util.List;

public class test2 
{

  // Defines edge structure
  static class Edge 
    {
    int src, dest, weight;

    public Edge(int src, int dest, int weight)
    {
      this.src = src;
      this.dest = dest;
      this.weight = weight;
    }

    @Override
      public String toString()
    {
        return "Edge [src=" + src + ", dest=" + dest + ", weight=" + weight + "]";
      }
  }
    
    public static void main(String[] args)
  {
    int V = 4;
    List<Edge> graphEdges = new ArrayList<Edge>(
                List.of(new Edge(0, 1, 10), new Edge(0, 2, 6),
                        new Edge(0, 3, 5), new Edge(1, 3, 15),
                        new Edge(2, 3, 4)));
    graphEdges.sort(new Comparator<Edge>() 
        {
      @Override public int compare(Edge o1, Edge o2)
      {
        System.out.println(graphEdges);
        System.out.println("o1="+o1.weight);
        System.out.println("o2="+o2.weight);
        return Integer.compare(o1.weight, o2.weight);
        
        /*Integer w = o1.weight - o2.weight;
        System.out.println(w);
        System.out.println(graphEdges);
        return o1.weight - o2.weight;*/
      }
    });
    System.out.println(graphEdges);
  }
}```
serene oceanBOT
#

This post has been reserved for your question.

Hey @flat drum! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

calm zinc
#

you mean you want it to be sorted in descending order?

#

for return Integer.compare(o1.weight, o2.weight); swap arguments, and you will get it sorted in descending order

flat drum
#

i see that, but i am asking when does the compare() swap its arguments, when it gets 1 right?

flat drum
calm zinc
#

compare() just compares, it doesn't swap anything

#

sort() method change positions if needed

#

and compare() is called from within of sort(), to figure out, should it swap elements or not

flat drum
#
[Edge [src=0, dest=1, weight=10], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=3, weight=5], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]
o1=6
o2=10
[Edge [src=0, dest=1, weight=10], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=3, weight=5], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=5
o2=6
[Edge [src=0, dest=1, weight=10], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=3, weight=5], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=15
o2=5
[Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=15
o2=6
[Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=15
o2=10
[Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=4
o2=10
[Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=4
o2=6
[Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15], Edge [src=2, dest=3, weight=4]]    
o1=4
o2=5
[Edge [src=2, dest=3, weight=4], Edge [src=0, dest=3, weight=5], Edge [src=0, dest=2, weight=6], Edge [src=0, dest=1, weight=10], Edge [src=1, dest=3, weight=15]]
flat drum
#

i mean, o1=10, o2=6 should be the order no?

calm zinc
#

new Edge(0, 1, 10), new Edge(0, 2, 6),

#

why you decided that first argument is from second index?

flat drum
#

because o1.weight comes out as 6?

calm zinc
#

i mean, o1=10, o2=6 should be the order no?
o1 comes out as 10, no?

flat drum
#

you can see if i print o1 its giving 6

#

and o2 as 10

#

and same way every iteration

calm zinc
#

it's up to sorting algorithm

flat drum
#

i having trouble understanding, is there any sorting algorithm being used other than the one i provide?

calm zinc
#

you don't provide any sorting algorithm

#

you provide only comparator to set the order, how to compare elements

#

sorting algorithms are like merge sort, quick sort, bubble sort etc

flat drum
#

i checked the definition, it uses merge sort i think

#

can you tell me what the comparator does when it gets 1 or -1

#

because its not really swapping based on 1 or -1 as it is using merge sort

#

how does returning 1 or -1 affect how it works

calm zinc
#

for o1 and o2 comparison returns
-1 if o1 < o2
0 if o1 == o2
1 if o1 > 02

#

Comparator just compares

#

if does nothing, just shows which object should go first

flat drum
#

i guess what i am trying to find out is what does 1,0,-1 do when it is returned

calm zinc
#

it says which object is smaller, or they are equal

flat drum
#

what does .sort do with this information

calm zinc
#

imagine sorting algorithm sorting integers. it takes 2 values, suppose 10 and 6. 6 should go first, so it swaps, and get 6, 10. Like,

if (elements[i] > elements[i-1])
  swap(elements, i, i-1);
```But how to compare objects? Imagine, we compare Imployee objects. How to compare? Compare by names? Compare by age maybe? Here we use comparators, to set the order how to compare them.
flat drum
#

so say for integers, does the swapping actually happen after it gets 1?

calm zinc
#

I guess so. But it really doesn't matter

#

  ...
  int result = comparator.compare(object1, object2)
  if (result == -1)
    swap elements
  ...
#

sorting algorithm call compare() method of comparator to figure out what element should go first

#

and if they go in wrong order, it swaps

#

if no need to swap - it doesn't

flat drum
#

thanks for your time and patience

serene oceanBOT
# flat drum thanks for your time and patience

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.