If you’re on .NET 3.5, you can use the following IEnumerable coolness: Edit: and here’s the corresponding VB.NET code: Second edit, in response to remarks that System.Random “isn’t threadsafe” and “only suitable for toy apps” due to returning a time-based sequence: as used in my example, Random() is perfectly thread-safe, unless you’re allowing the routine … Read more
You would need a custom sort using script to achieve what you want. I’ve just made use of generic match_all query for my query, you can probably go ahead and add your query logic there, but the solution that you are looking for is in the sort section of the below query. Make sure that status is a keyword type Custom Sorting Based on Values … Read more
It’s a trick question. If you just want the maximum, (or indeed, the kth value for any k, which includes finding the median), there’s a perfectly good O(n) algorithm. Sorting is a waste of time. That’s what they want to hear. As you say, the algorithm for maximum is really trivial. To ace a question like this, you … Read more
We’re building a website using the Pentaho CTools library, which has a graphical dashboard editor which writes out JSON-format files for part of the dashboard. I’d like to apply a transform to these files before check-in to git in order to sort them by key and then by the value of certain keys. The purpose … Read more
Two arguments come to my mind: Quicksort/Introsort is more flexible: Quicksort and Introsort work well with all kinds of data. All you need for sorting is the possibility to compare items. This is trivial with numbers but you can sort other data as well. Radix sort on the other hand just sorts things by their … Read more
In Ruby 2.1 it is simple:
In C, you can use the built in qsort command: see: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/ To answer the second part of your question: an optimal (comparison based) sorting algorithm is one that runs with O(n log(n)) comparisons. There are several that have this property (including quick sort, merge sort, heap sort, etc.), but which one to use depends on your use … Read more
Looks like you’re a little bit confused about all that stuff. operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it. So, you can’t use key=a[x] there, because python has no idea what x is. … Read more
Shortest Code Example: Generally Speaking Explained: zip the two lists. create a new, sorted list based on the zip using sorted(). using a list comprehension extract the first elements of each pair from the sorted, zipped list. For more information on how to set\use the key parameter as well as the sorted function in general, take a look at this.
Bucket sort and radix sort are close cousins; bucket sort goes from MSD to LSD, while radix sort can go in both “directions” (LSD or MSD). How do both algorithms work, and in particular how do they differ?