sorting

ordering a list of files in a folder using php

I am using the code below to display all the files from a directory in a drop down menu. Does anyone know how to make this alphabetical? I presume it has something to do with the sort function, I just can't figure out how! <?php $dirname = "images/"; $images = scandir($dirname); $dh = opendir($dirname); while ($file = readdir($dh)) { i...

Best sorting algorithms for C# / .NET in different scenarios

What are the best algorithms for sorting data in C#? Is there one sorting algorithm that can handle 80% of sorts well? Please give code examples if applicable. ...

C# equivalent of std::sort and std::unique

I have a list of integers in C#. I wish to remove duplicates. In C++ I would run it through the std::sort and then std::unique algorithms for a very efficient way of obtaining the unique list. What's the best way to do the same thing in C#? In other words, I'm looking for a more elegant way to do the following code: private static...

Do you write code to sort a list these days?

People in java/.net world has framework which provides methods for sorting a list. In CS, we all might have gone through Bubble/Insertion/Merge/Shell sorting algorithms. Do you write any of it these days? With frameworks in place, do you write code for sorting? Do you think it makes sense to ask people to write code to sort in an inte...

How do I sort arrays using vbscript?

The question says it all really, but... I'm scanning through a file looking for lines that match a certain regex pattern, and then I want to print out the lines that match but in alphabetical order. I'm sure this is trivial but vbscript isn't my background my array is defined as Dim lines(10000) if that makes any difference, and I'm...

Chaining of ordering predicates (e.g. for std::sort)

You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted. However, sometimes (enough that I've hit this several times), you want to be able to chain "primitive" comparisons. A trivial example would be if you were sorting a collectio...

What is a bubble sort good for?

Do bubble sorts have any real world use? Every time I see one mentioned, it's always either: A sorting algorithm to learn with. An example of a sorting algorithm not to use. ...

Custom Sorting Of A Bound DataGridView

I have a DataGridView bound to a DataTable. I have one column that's a pseudo-int -- you know the kind, where most of the time it has integers but sometimes instead there's an N/A. This column is a varchar, but I want to have it sort like an int column, treating the N/A as a -1. The DataGridView provides for this -- if it's not bound to...

The application sorts strings differently than the database

I am trying to sort a list of products by their name in a .Net application written in C#, to obtain the same list that I would get from an SQL Server database through an order by: select * from Products order by ProductName Unfortunately, the application sorting behaves differently than the database sorting. It is probably related to th...

What is the most elegant way of bubble-sorting in F#?

What is the most elegant way of bubble-sorting in F#? UPDATE As pointed out in one of the answers, bubble sorting isn't efficient in a functional language to begin with. A humourously-cynical commenter also pointed out that bubble sorting is only appropriate when the list is small and it's almost sorted anyway. However, I'm curious t...

Python - sort a list of nested lists

I have input consisting of a list of nested lists like this: l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] I want to sort this list based on the sum of all the numbers in the nested lists... so, the values I want to sort by of l would look like this: [39, 6, 13, 50] Then I want to sort based on these. So the...

WaitCursor on sort in DataGridView

I am using the standard .Net 2.0 DataGridView with sort mode of automatic on the column. It is very very slow (which should probably be another question on how to speed it up) but I can't seem to find an event or combination of events that will maintain a WaitCursor while this sort operation is being performed. Ideas? ...

How to best sort a portion of a circular buffer?

I have a circular, statically allocated buffer in C, which I'm using as a queue for a depth breadth first search. I'd like have the top N elements in the queue sorted. It would be easy to just use a regular qsort() - except it's a circular buffer, and the top N elements might wrap around. I could, of course, write my own sorting implemen...

Multiple ordered lists boiled down to one list, where order is relative

I have multiple ordered lists. Unfortunately, the order of the items isn't a simple alpha or numeric comparison, otherwise this is trivial. So what I have is something like: List #1 List #2 List #3 groundhog groundhog easter mothersday mayday mothersday midsummer laborday halloween christmas ...

Flex datagrid universal numeric sorting function?

I am using a Flex dataGrid, and need to sort some of my columns numerically. Looking at the sortCompareFunction, it seems like i need to create a different function for each column that i want to sort, because my sort function has to know what field it is sorting on. Is there any way that I can pass the field to be sorted on into the ...

What is the fastest sort algorithm for 0-65535 integers?

I have to sort a number of integers, which can have values between 30.000.000 and 350.000.000. There will be between 0 and 65.535 integers, with the average count being 20.000. RAM usage is irrelevant and speed only is important. Later on i will also have to split them into groups, with the divide always being set whenever the gap betwe...

How can I print the lines in STDIN in random order in Perl?

I want to do the inverse of sort(1) : randomize every line of stdin to stdout in Perl. ...

JavaScript: Efficiently replace all accented characters in a string?

For a poor man's implementation of near-collation-correct sorting on the client side I need a JavaScript function that does efficient single character replacement in a string. Here is what I mean (note that this applies to German text, other languages sort differently): native sorting gets it wrong: a b c o u z ä ö ü collation-correct...

Running Time of Random Sort

How would you describe the running time of this sort, given a function sorted which returns True if the list is sorted that runs in O(n): def sort(l): while not sorted(l): random.shuffle(l) Assume shuffling is perfectly random. Would this be written in big-O notation? Or is there some other way of categorizing algorithms with ra...

C# List<> Sort by x then y

Similar to this question, but in 2.0, we want to sort by one element, then another. we want to achieve the functional equivalent of SELECT * from Table ORDER BY x, y We have a class that contains a number of sorting functions, and we have no issues sorting by one element. For example: public class MyClass { public int x;...