sorting

Is it faster to sort a list after inserting items or adding them to a sorted list.

If I have a sorted list (say quicksort to sort), if I have a lot of values to add, is it better to suspend sorting, and add them to the end, then sort, or use binary chop to place the items correctly while adding them. Does it make a difference if the items are random, or already more or less in order? ...

Best generic strategy to group items using multiple criteria

Hello, I have a simple, real life problem I want to solve using an OO approach. My harddrive is a mess. I have 1.500.000 files, duplicates, complete duplicate folders, and so on... The first step, of course, is parsing all the files into my database. No problems so far, now I got a lot of nice entries which are kind of "naturaly groupe...

Sorting a list into multiple vertical columns

Does anyone have a good algorithm for re-sorting an array of values (already pre-sorted) so that they can be displayed in multiple (N) columns and be read vertically? This would be implemented in .Net but I'd prefer something portable and not some magic function. A good example of it working is the ASP.Net CheckBoxList control rendering...

lists:usort for nth element in tuple

I need to sort tuples according to the second element of each tuple but apparently usort/1 only works with the first element. So I have to swap the elements, usort them and swap back.Is there an easier way?Also is there a way to sort in descending order (I know sorting and reversing can be done, but just want to know). ...

Writing resultset to file with sorted output

I want to put "random" output from my result set (about 1.5 mil rows) in a file in a sorted manner. I know i can use sort by command in my query but that command is "expensive". Can you tell me is there any algorithm for writing result set rows in a file so the content would be sorted in the end and can i gain in performance with this? ...

C# List<> OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<>. For the sake of this example lets say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression? List<Person> people = PopulateList(); people.OrderBy(???? => ?????) ...

Is there a sorted collection type in .NET?

I'm looking for a container that keeps all its items in order. I looked at SortedList, but that requires a separate key, and does not allow duplicate keys. I could also just use an unsorted container and explicitly sort it after each insert. Usage: Occasional insert Frequent traversal in order Ideally not working with keys separate ...

jquery table header sort

Anybody know of a plugin, or a built in function to make the columns in a table sortable? i.e. I click on the column header and it sorts the rows by that column? ...

Best way to list files in Java, sorted by Date Modified?

I want to get a list of files in a directory, but I want to sort it such that the oldest files are first. My solution was to call File.listFiles and just resort the list based on File.lastModified, but I was wondering if there was a better way. Edit: My current solution, as suggested, is to use an anonymous Comparator: File[] files = ...

Which sorting algorithm is used by .net in IComparer

Do any one know which sorting algorithm is used by .net when we implement IComparer in our class? ...

How to map sorted index back to original index for collection I'm sorting.

I've got a collection (List<Rectangle>) which I need to sort left-right. That part's easy. Then I want to iterate through the Rectangles in their original order, but easily find their index in the sorted collection. indexOf() won't work, since I may have a number of equal objects. I can't help feeling there should be an easy way to do th...

What should students be taught first when first learning sorting algorithms?

If you were a programming teacher and you had to choose one sorting algorithm to teach your students which one would it be? I am asking for only one because I just want to introduce the concept of sorting. Should it be the bubble sort or the selection sort? I have noticed that these two are taught most often. Is there another type of sor...

SQL Query - Using Order By in UNION

How can one programmatically sort a union query when pulling data from two tables? For example, SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1 Throws an exception Note: this is be attempted on MS Access Jet database engine ...

Sort arrays of primitive types in descending order

I've got a large array of primitive types (double). How do I sort the elements in descending order? Unfortunately the Java API doesn't support sorting of primitive types with a Comparator. One workaround would be to sort and then reverse: double[] array = new double[1048576]; ... Arrays.sort(array); // reverse the array for(int i=0;i<...

How can I sort members by name in Java?

I want to sort members by name in the source code. Is there any easy way to do it? I'm using NetBeans, but if there is another editor that can do that, just tell me the name of it. ...

Finding "best matching key" for a given key in a sorted STL container

Problem I have timestamped data, which I need to search based on the timestamp in order to get the one existing timestamp which matches my input timestamp the closest. Preferably this should be solved with the STL. boost::* or stl::tr1::* (from VS9 with Featurepack) are also possible. Example of timestamped data: struct STimestampedDat...

Which sort algorithm works best on mostly sorted data?

Which sort algorithm works best on mostly sorted data? ...

Whats the best method to maintain or measure how well sorted a collection is so we can choose best sort algorithm?

Inspired by this question The choice of which algorithm to use to sort a collection can be made better if we know ahead of time how well sorted a collection is. Is there a way we can measure (or maintain a measurement) of how well sorted the collection is? Can we do this in such a way that the cost of maintaining or measuring how well s...

Sorting a tuple that contains tuples

I have the following tuple, which contains tuples: MY_TUPLE = ( ('A','Apple'), ('C','Carrot'), ('B','Banana'), ) I'd like to sort this tuple based upon the second value contained in inner-tuples (i.e., sort Apple, Carrot, Banana rather than A, B, C). Any thoughts? ...

set initial sort order in Silverlight DataGrid?

When I first load data into a Silverlight DataGrid control, how can I make the screen look exactly as if the user had just clicked the header of the first column? In other words, the data should be sorted in ascending order according to that column's values, AND the little sort arrow should be displayed in the first column's header. As...