How to sort with lambda in Python
Use
a = sorted(a, key=lambda x: x.modified, reverse=True)
# ^^^^
On Python 2.x, the sorted
function takes its arguments in this order:
sorted(iterable, cmp=None, key=None, reverse=False)
so without the key=
, the function you pass in will be considered a cmp
function which takes 2 arguments.
sort by and then in c++ using lambda functions
It looks too me like you want a lexicographic sort on (y, x)
1. You can leverage the library function std::tie
. That one returns a tuple of references, and a std::tuple
has a less-than operator which performs lexicographic comparison. So you only need to specify the order in which you want the items compared.
Here's how it would look for std::vector
(your go to container type in C++, always start with std::vector
):
std::vector<Point> my_list;
// Fill my_list
std::sort(begin(my_list), end(my_list), [](auto const& l, auto const& r){
return std::tie(l.y, l.x) < std::tie(r.y, r.x);
});
1 - I'm basing this on the method names only, so this may not be what you are really after.
Sorting a LinkedList with a lambda expression
import java.util.stream.Collectors;
public List<Integer> methodName(int length, int min, int max) {
LinkedList<Integer> intll = new LinkedList<>();
for (int i =0; i<length;i++)
intll.add(ThreadLocalRandom.current().nextInt(min,max+1));
return intll.stream().sorted((a, b) -> a - b).collect(Collectors.toList());
}
if you want to change the sort order, use b - a instead of a - b
How to sort integer array in ascending and descending order using lambda only in java
You could sort the input of type Integer[]
as :
Integer[] arr2 = new Integer[] {54,432,53,21,43};
Arrays.sort(arr2, Comparator.reverseOrder());
or possibly with primitive types as :
int[] arr2 = new int[]{54, 432, 53, 21, 43};
int[] sortedArray = Arrays.stream(arr2)
.boxed()
.sorted(Comparator.reverseOrder()) // just use 'sorted()' for ascending order
.mapToInt(Integer::intValue)
.toArray();
or further using a trick from one of the existing answers (do note that it should be cautiously used with boundary values though) :
int[] sortedArray = Arrays.stream(arr2)
.map(i -> -i).sorted().map(i -> -i) // just use 'sorted()' for ascending order
// Edit - use map(i -> ~i).sorted().map(i -> ~i) to be safe from the issue with Integer.MIN_VALUE
.toArray();
Edit: For an in-place ascending order sort, you just need to perform :
int[] arr2 = new int[]{54, 432, 53, 21, 43};
Arrays.sort(arr2);
Related Topics
Knight's Tour Backtrack Implementation Choosing the Step Array
C++ Gdi::Bitmap to Png Image in Memory
Boost::Asio Synchronous Client with Timeout
Should I Use a C++ Reinterpret_Cast Over a C-Style Cast
How to Store a Reference of a Singleton Class
Can the Hwnd from Createwindow/Createdialog Be Getmessage'D from Another Thread
How to Know the Right Max Size of Vector? Max_Size()? But No
Breaking Out from Socket Select
C/C++: Casting Away Volatile Considered Harmful
Array of Size Defined by Not Constant Variable
Why Can't I Use Inheritance to Implement an Interface in C++
Do I Get a Finished Slot If I Start Qprocess Using Startdetached
String Plus Char - What Is Happening
How to Make a Recursive Rule in Boost Spirit X3 in VS2017
How Would One Call Std::Forward on All Arguments in a Variadic Function