Posts

Showing posts with the label Sorting

Bash Associative Array Sorting By Value

Answer : You can easily sort your output, in descending numerical order of the 3rd field: for k in "${!authors[@]}" do echo $k ' - ' ${authors["$k"]} done | sort -rn -k3 See sort(1) for more about the sort command. This just sorts output lines; I don't know of any way to sort an array directly in bash. I also can't see how the above can give you names ("Pushkin" et al.) as array keys. In bash, array keys are always integers. Alternatively you can sort the indexes and use the sorted list of indexes to loop through the array: authors_indexes=( ${!authors[@]} ) IFS=$'\n' authors_sorted=( $(echo -e "${authors_indexes[@]/%/\n}" | sed -r -e 's/^ *//' -e '/^$/d' | sort) ) for k in "${authors_sorted[@]}"; do echo $k ' - ' ${authors["$k"]} done Extending the answer from @AndrewSchulman, using -rn as a global sort option reverses all columns. In this example, author...

Alphabetical Sorting Of A Sequence Of Names

Image
Answer : Bubble sorter, which I adapt from my modification to David's answer to my question at Trying to eliminate stack overflow during recursion. The \sortlist macro is the bubble sorter (from the referenced answer, but with and rather than , as the list seperator). However, it leaves the result in the form of Last Name, First and ... . I had to add the \rework macro to make it First Last Name and employ \whichsep to choose whether a , or and should be inserted between names, depending on their placement in the list. No packages required! \documentclass[10pt]{article} \newcommand\alphabubblesort[1]{\def\sortedlist{}% \expandafter\sortlist#1 and \cr and \relax \expandafter\rework\sortedlist and \relax} \def\sortlist#1and #2and #3\relax{% \let\next\relax \ifx\cr#2\relax% \edef\sortedlist{\sortedlist#1}% \else \picknext#1!and #2!\relax% \if F\flipflop% \edef\sortedlist{\sortedlist#1and }% \def\next{\sortlist#2and #3\relax}% \else...

Best Way To Make WPF ListView/GridView Sort On Column-header Clicking?

Answer : I wrote a set of attached properties to automatically sort a GridView , you can check it out here. It doesn't handle the up/down arrow, but it could easily be added. <ListView ItemsSource="{Binding Persons}" IsSynchronizedWithCurrentItem="True" util:GridViewSort.AutoSort="True"> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" util:GridViewSort.PropertyName="Name"/> <GridViewColumn Header="First name" DisplayMemberBinding="{Binding FirstName}" util:GridViewSort.PropertyName="FirstName"/> <GridViewColumn Header="Date of birth" ...

400x Sorting Speedup By Switching A.localeCompare(b) To (ab?1:0))

Answer : A great performance improvement can be obtained by declaring the collator object beforehand and using it's compare method. EG: const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }); arrayOfObjects.sort((a, b) => { return collator.compare(a.name, b.name); }); Here's a benchmark script comparing the 3 methods: const arr = []; for (let i = 0; i < 2000; i++) { arr.push(`test-${Math.random()}`); } const arr1 = arr.slice(); const arr2 = arr.slice(); const arr3 = arr.slice(); console.time('#1 - localeCompare'); arr1.sort((a, b) => a.localeCompare( b, undefined, { numeric: true, sensitivity: 'base' } )); console.timeEnd('#1 - localeCompare'); console.time('#2 - collator'); const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }); arr2.sort((a, b) => collator.compare(a, b)); console.timeEnd('#2 - collator'); con...