-3

So I'm currently learning 2D arrays in c++, I was solving this question which had a 2D array and we were to use sort inbuilt sort function of c++ with comparator, here's the code:

  int getLights(vector<vector<int>>& lights) {
  sort(lights.begin(),lights.end(),
             [](const auto& a, const auto& b){
                 return a[0]==b[0] ? a[1]>b[1] : a[0]<b[0];
             });
        

I'm not able to understand how this sort function works, can anyone help me get the output with this input:

lights = [[5,5],[6,3],[3,6]]
Vaibhav
  • 7
  • 2
  • what exactly don't you understand? What is "the output"? Please show a [mre] – Alan Birtles Sep 09 '22 at 06:48
  • I don't understand what "[](const auto& a, const auto& b){ return a[0]==b[0] ? a[1]>b[1] : a[0] – Vaibhav Sep 09 '22 at 06:52
  • @Vaibhav It uses a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda). See dupe: [What is a lambda expression in C++11?](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11). Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. – Jason Sep 09 '22 at 07:01

2 Answers2

1

The C++'s built in sort that you are using takes the first and last element of the array to be sorted to know what has to be sorted. The third argument is a "comparator", i.e. a function that takes two arguments and returns true if the first argument should come before the second.

In your case a vector a comes before other b if its first element a[0] is smaller the other's b[0], and if they are the same then a comes first if a[1] > b[1]. To archive this logic the conditional operator (E1 ? E2 : E3) is used, which checks the truth value of the first expresión and returns the second if it's true and the third expresión if it's false.

David Rubio
  • 144
  • 10
  • I got what you're trying to say, but in my case what is the value that my function will return if a[0]==b[0]. – Vaibhav Sep 09 '22 at 07:56
  • @Vaibhav If a[0]==b[0] then the lambda function will return the value of the expression a[1] > b[1]. All in all, if you have a list of 2D points that can be represented in a 2D graph it will sort the points left to right and, within the same "column" (i.e. a[0]==b[0]), top to bottom. – David Rubio Sep 09 '22 at 09:04
-1

C++ STL provides a function sort that sorts a vector or array (items with random access)

It generally takes two parameters, the first being the point of the array/vector from where the sorting needs to begin and the second parameter being the length to which we want the array/vector to get sorted. The third parameter is optional and can be used in cases where we want to sort the elements lexicographically.

By default, the sort() function sorts the elements in ascending order.

How to sort in a particular order?

We can also write our own comparator function and pass it as a third parameter. This “comparator” function returns a value; convertible to bool, which basically tells us whether the passed “first” argument should be placed before the given “second” argument or not.

Source: Sort C STL

The function you have written as the 3rd argument act as a comparator (it is a lambda expression), which returns a boolean value.

return a[0]==b[0] ? a[1]>b[1] : a[0]<b[0];

This statement basically compares the elements of a and b, if 1st element is the same check for the second element. It returns the smallest of the two numbers with respect to the 1st element of both arrays.

Output: [[3,6],[5,5],[6,3]]
Sprivro
  • 37
  • 9
  • 1
    Nice answer but the mention of lexicographical sorting is strange. A comparator is used when we want an ordering that is different from the default, or when there is no default. Lexicographic doesn't have anything to do with it. For example the default sorting for elements that are strings or vectors is lexicographical/ – john Sep 09 '22 at 07:03
  • Thanks, by lexicographically I meant if we have an array of characters/strings, we can sort it in dictionary order/lexicographically. – Sprivro Sep 09 '22 at 07:08
  • @Sprivro But my point is that a) you get that behaviour any way in many cases, and b) there are other reasons to use a comparator. – john Sep 09 '22 at 07:09
  • True, but there are more reasons, but I kept my answer for basic understanding – Sprivro Sep 09 '22 at 07:10
  • @Sprivro Thanks, it's more honest now :) And I think It's a legitimate answer since you added your own thoughts and explanations. – Lukas-T Sep 09 '22 at 07:12