Custom key-sort a flat associative based on another array

Just use array_merge or array_replace. array_merge works by starting with the array you give it (in the proper order) and overwriting/adding the keys with data from your actual array: $customer[‘address’] = ‘123 fake st’; $customer[‘name’] = ‘Tim’; $customer[‘dob’] = ’12/08/1986′; $customer[‘dontSortMe’] = ‘this value doesnt need to be sorted’; $properOrderedArray = array_merge(array_flip(array(‘name’, ‘dob’, ‘address’)), $customer); … Read more

How to sort a list of dictionaries by a value of the dictionary in Python?

The sorted() function takes a key= parameter newlist = sorted(list_to_be_sorted, key=lambda d: d[‘name’]) Alternatively, you can use operator.itemgetter instead of defining the function yourself from operator import itemgetter newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’)) For completeness, add reverse=True to sort in descending order newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’), reverse=True)

Sort a Table[] in Lua

A table in Lua is a set of key-value mappings with unique keys. The pairs are stored in arbitrary order and therefore the table is not sorted in any way. What you can do is iterate over the table in some order. The basic pairs gives you no guarantee of the order in which the … Read more

java: Arrays.sort() with lambda expression

The cleanest way would be: Arrays.sort(months, Comparator.comparingInt(String::length)); or, with a static import: Arrays.sort(months, comparingInt(String::length)); However, this would work too but is more verbose: Arrays.sort(months, (String a, String b) -> a.length() – b.length()); Or shorter: Arrays.sort(months, (a, b) -> a.length() – b.length()); Finally your last one: Arrays.sort(months, (String a, String b) -> { return Integer.signum(a.length() – … Read more

Hashtables and key order

There is no built-in solution in PowerShell V1 / V2. You will want to use the .NET System.Collections.Specialized.OrderedDictionary: $order = New-Object System.Collections.Specialized.OrderedDictionary $order.Add(“Switzerland”, “Bern”) $order.Add(“Spain”, “Madrid”) $order.Add(“Italy”, “Rome”) $order.Add(“Germany”, “Berlin”) PS> $order Name Value —- —– Switzerland Bern Spain Madrid Italy Rome Germany Berlin In PowerShell V3 you can cast to [ordered]: PS> [ordered]@{“Switzerland”=”Bern”; “Spain”=”Madrid”; … Read more

Sort a list of tuples by second value, reverse=True and then by key, reverse=False

The following works with your input: d = [(‘B’, 3), (‘A’, 2), (‘A’, 1), (‘I’, 1), (‘J’, 1)] sorted(d,key=lambda x:(-x[1],x[0])) Since your “values” are numeric, you can easily reverse the sort order by changing the sign. In other words, this sort puts things in order by value (-x[1]) (the negative sign puts big numbers first) … Read more

How can I sort a ListBox using only XAML and no code-behind?

Use a CollectionViewSource: <CollectionViewSource x:Key=”SortedItems” Source=”{Binding CollectionOfStrings}” xmlns:scm=”clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase”> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName=”SomePropertyOnYourItems”/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <ListBox ItemsSource=”{Binding Source={StaticResource SortedItems}}”/> You might want to wrap your strings in a custom VM class so you can more easily apply sorting behavior.

Sorting int array in descending order [duplicate]

For primitive array types, you would have to write a reverse sort algorithm: Alternatively, you can convert your int[] to Integer[] and write a comparator: public class IntegerComparator implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1); } } or use Collections.reverseOrder() since it only works on non-primitive array types. and … Read more

Sort array by value alphabetically php

You want the php function “asort”: http://php.net/manual/en/function.asort.php it sorts the array, maintaining the index associations. Edit: I’ve just noticed you’re using a standard array (non-associative). if you’re not fussed about preserving index associations, use sort(): http://php.net/manual/en/function.sort.php