sorting

How can we sort the list of values to our requirement?

I have a list of values from 1 to 10, and I want to sort those values in the order 6, 7, 8, 1, 2, 0, 3, 9, 5, 10. How can I sort like this using a comparator? import java.util.ArrayList; import java.util.List; public class SortTest { public static void main(String[] args) { int a[] = { 6, 7, 8, 1, 2, 0, 3, 9, 4, 5, 10 }; List lis...

Strange behaviour of sort

I have this piece of script : #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @arr = ( { name => 'foo', value => 123, }, { name => 'bar', value => 'nan', }, { name => 'foobar', value => 456, }, ); @arr = sort {$a->{value} <=> $b->{value} } @arr; ...

sorting by arbitrary lambda in Python

How can I sort a list by a key described by an arbitrary function? For example, if I have: mylist = [["quux", 1, "a"], ["bar", 0, "b"]] I'd like to sort "mylist" by the second element of each member, e.g. sort(mylist, key=lambda x: x[1]) how can I do this? thanks. ...

Is F# ResizeArray sortBy stable?

Does the function ResizeArray.sortBy do a stable sort, in the sense that it does not change the order of elements which have the same value for the key function? And if not, how to write a stable sort in F#? ...

Javascript sorting to match SQL Server sorting

Can anyone point me towards a sorting algorithm in javascript that would sort the same way SQL Server does (for nvarchar/unicode columns)? For reference, my previous question about this behavior can be found here: http://stackoverflow.com/questions/3213717/sql-server-2008-different-sort-orders-on-varchar-vs-nvarchar-values Rather than ...

Sorting on the last field of a line

What is the simplest way to sort a list of lines, sorting on the last field of each line? Each line may have a variable number of fields. Something like sort -k -1 is what I want, but sort(1) does not take negative numbers to select fields from the end instead of the start. I'd also like to be able to choose the field delimiter too....

LINQ .OrderBy single letter then by another letter

Hello Im trying to make a special orderby with Linq. It has to order by S then P then NB then V and then NC. Im not sure if its the best way to de this but this is what i have: repeater.DataSource = query.OrderBy(p => p.Title ?).ThenBy(?).ThenBy(?); ...

Sort an array with numeric strings

Hello I have an array of persons, and i am trying to sort them by age using a sort descriptor. The age field in a patient is a string so when calling: ageSorter = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES]; [personList sortUsingDescriptors:[NSArray arrayWithObject:ageSorter]]; It sorts them but 100 appears first becau...

Sorting dicts (contained in lists) alphanumerically in Python

I'm having an issue with sorting a list that contains a dict. Currently I am sorting it by a key called 'title' with the following line: list.sort(key=operator.itemgetter('title')) The problem with this is that some of my data gets sorted looking like this: title_text #49 title_text #5 title_text #50 How would I go about sorting it...

PHP and wordpress: Sorting Menu items

I'm working on a wordpress site, customizing the menu. I'm no php whiz, and I want the menu to sort according to the numerical order, not desc alphabetically. <div id="navigation"> <ul> <?php wp_list_pages('sort_column=post_title&sort_order=desc&title_li=&depth=1&')?> </ul> </div><!-- end id:navigation --> Any ideas how I do ...

Sort multidimensional array by multiple keys

I'm trying to sort a multidimensional array by multiple keys, and I have no idea where to start. I looked at uasort, but wasn't quite sure how to write a function for what I need. I need to sort by the state, then event_type, then date. My array looks like this: Array ( [0] => Array ( [ID] => 1 ...

What is the best sort algorithm for a random set of floats?

A colleague of mine just put that question out in the air this afternoon, and somewhat left me curious. I'm versed with sorting algos, but lacking a formal degree in compsci / compeng (something I'm sorta averse to admitting), can't really place my finger on this one. :p And oh yeah, this is mildly in the context of a C#/.NET implementa...

Search for the smallest element in a list after some value

Consider a list of integers <1,5,10> (assume sorted in ascending fashion). Given an integer, say, key = 6, is there a utility method that returns the smallest element after key (in this case it would be 10)? NB: Looping through the elements in the list and comparing it with key is an obvious way to do it, but I'm just wondering if ther...

Change the way the Django Admin sorts integers?

The model has an IntegerField with null=True, blank=True, so it allows the field to have a value of None. When the column is sorted, the Nones are always treated as the lowest values. Is it possible to make Django treat them as the highest values? So when sorted in descending order, instead of: 100 43 7 None It would go: None 100 43 7...

Javascript Sort SELECT Elements

Is there an easy way to sort a list of HTML Select elements in Javascript? I would want it sorted by their names. Each Select element has other fields like title and value. Would I have to use arrays? ...

Sort List After Last Name, then First Name?

I'm planning on building a mySQL table that requires a student's name which looks like this: Last Name + Father's First Name Initials + First Name Example: Beavers L. Scott James (father has one first name, son has two first names) Beavers L. D. Scott (father has two first names) Instead of requiring to fill in 3 input fields, I o...

Winforms DataGrid with bound ComboBox column - How to sort by DisplayMember?

Scenario: Winforms DataGrid control in which one of the columns is a comboBox control (NB: not datagridview). The DataSource for the DataGrid is a dataView, with essentially two ID fields, lets say OrderID, CurrencyID; i show the CurrencyName in the combo and grid, but store the CurrencyID. So, the DisplayMember for the combobox is Curr...

Sorting a StringListProperty based on position of element in app engine

I have model which looks like this: class Example (db.Model) : name = db.StringProperty() tags = db.StringListProperty() I first query for a tag to get the list of entities which have them: results = Example.all().filter("tags =", tagSearch).fetch(100) This gives me a list of entities containing the "tagSearch" in their "tags" li...

How to sort an event table by time (have different timezones)?

I have a table that is structured the following way (that i can't change): EventName, LocalTime, Timezone Data would be something like this: Event1, 10:00, ET Event2, 11:00, ET Event3, 12:00, ET Event4, 10:00, CT how can i write a sql to sort this by actual time so result would be like: Event1, 10:00, ET Event2, 11:00, ET Event4, 1...

PHP: sorting array - list of ftp files?

hey guys, i'm connecting to a ftp-server and displaying all items in a list. i want that list to be ordered alphabetically. shouldn't that do it? // get contents of the current directory $contents = ftp_nlist($conn_id, $path); sort($contents); that's a part of the script! // get contents of the current directory $con...