sorting

How do you sort a C# dictionary by value?

I often have a Dictionary of keys & values and need to sort it by value. For example, I have a hash of words and their frequencies, and want to order them by frequency. There's SortedList which is good for a single value (frequency), but I want to map it back to the word. SortedDictionary orders by key, not value. Some resort to a cust...

graph serialization

I'm looking for a simple algorithm to 'serialize' a directed graph. In particular I've got a set of files with interdependencies on their execution order, and I want to find the correct order at compile time. I know it must be a fairly common thing to do - compilers do it all the time - but my google-fu has been weak today. What's the 'g...

Merge Sort a Linked List

I was recently brushing up on some fundamentals and found merge sorting a linked list to be a pretty good challenge. If you have a good implementation then show it off here. ...

Rigor in capturing test cases for unit testing

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers> unsorted, bool ascending); We pass in an unsorted list of numbers and a boolean specifying ascending or descending sort order. In return, we get a sorted list of numbers. In my experience, some people are better at capturing boun...

How does one rank an array (sort) by value? *With a twist*

Hi all, I need to take an array and sort it in c/c++, replacing each value with its' "rank", an integer that corresponds to its position after sort. Heres an example: Input: 1,3,4,9,6. Output: 1, 2, 3, 5, 4. So, I need to replace each value with its position relative to all the other values. I hope that makes sense, but its late, so...

Sorting an IList in C#

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it. Turns out the IList interface doesn't have a sort method built in. I ended up using the ArrayList.Adapter(list).Sort(new MyComparer()) method to solve the problem but it just seemed a bit "gh...

An O(1) Sort ~~~

Before you stone me for being a heretic, There is a sort that proclaims to be O(1), called "Bead Sort" (http://en.wikipedia.org/wiki/Bead_sort) , however that is pure theory, when actually applied I found that it was actually O(N * M), which is pretty pathetic. That said, Lets list out some of the fastest sorts, and their best case and ...

How do I write a sort worse than O(n!)

I wrote an O(n!) sort for my amusement that can't be trivially optimized to run faster without replacing it entirely. [And no, I didn't just randomize the items until they were sorted]. How might I write an even worse Big-O sort, without just adding extraneous junk that could be pulled out to reduce the time complexity? http://en.wi...

Why doesn't **sort** sort the same on every machine?

Using the same sort command with the same input produces different results on different machines. How do I fix that? ...

Natural (human alpha-numeric) sort in Microsoft SQL 2005

We have a large database on which we have DB side pagination. This is quick, returning a page of 50 rows from millions of records in a small fraction of a second. Users can define their own sort, basically choosing what column to sort by. Columns are dynamic - some have numeric values, some dates and some text. While most sort as expe...

Natural Sorting algorithm

How do you sort an array of strings naturally in different programming languages? Post your implementation and what language it is in in the answer. ...

Comparison of Lat, Long Coordinates

I have a list of more than 15 thousand latitude and longitude coordinates. Given any X,Y coordinates, what is the fastest way to find the closest coordinates on the list? ...

How do I sort a list of strings in Python?

What is the best way of creating an alphabetically sorted list in Python? ...

Best way to sort an array in delphi

Say I have an array of records which I want to sort based on one of the fields in the record. What's the best way to achieve this? TExample = record SortOrder : integer; SomethingElse : string; end; var SomeVar : array of TExample; ...

How to do multi-column sorting on a Visual Basic 6 ListView?

I am working in Visual Basic 6 and need to sort by multiple columns in a ListView. For example, sorting a list of music tracks by artist, then album, then track number. As far as I know, VB6 does not support this out of the box. Here are the suggestions I have already heard: Sort the data in a SQL table first and display the data in...

What is the most efficient way to sort an Html Select's Options by value, while preserving the currently selected item?

I have jQuery but I'm not sure if it has any built-in sorting helpers. I could make a 2d array of each item's text, value, and selected properties, but I don't think that javascript's built in Array.sort() would work correctly. ...

xslt: apply-templates in reverse order

say i have this given xml file <root> <node>x</node> <node>y</node> <node>a</node> </root> and i want the following to be displayed ayx using something similar to <xsl:template match="/"> <xsl:apply-templates select="root/node"/> </xsl:template> <xsl:template match="node"> <xsl:value-of select="."/> </xsl:templ...

In Python, how can you easily retrieve sorted items from a dictionary?

Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key? For example, you had this: d = {...

Rosetta Stone - Sorting Arrays

I thought I would pose a question I'm describing as a "Rosetta Stone". That is to say, I am posing a question, and would like to see answers given for a number of different languages so that someone moving from one language to another can see how some typical operation is done. I have always found these types of comparisons to be very ...

In Python how do I sort a list of dictionaries by values of the dictionary?

I got a list of dictionaries and want that to be sorted by a value of that dictionary. This [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted by name, should become [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}] ...