sorting

What's a good way to insert something in the middle of a list?

This is probably a simple question. Let's say I have a small list with around 20-50 entries or so. Something like: class Item { int ItemNumber; int OrderNumber; string Name; } stored in something like List<Item> This is stored either in a generic list or array in where the OrderNumber goes from 1, 2,3,4,....50. To makes thin...

Best way to optimize dataset that contains linestrings. Some lines start and end at same coordinates.

THE SETUP I have a table which contains linestrings. Linestrings are made up of multiple geographic points. Each point is made up of a latitude and longitude. Note: the linestring value is stored as TEXT in the database. So one row in the table might look like this: id: an integer linestring: x1, y2, x2, y2, x3, y3, x4, y4 THE PROBLEM ...

In-Place Radix Sort

This is a long text. Please bear with me. Boiled down, the question is: does someone know a workable in-place radix sort algorithm? Preliminary I've got a huge number of small fixed-length strings that only use the letters “A”, “C”, “G” and “T” (yes, you've guessed it: DNA) that I want to sort. At the moment, I use std::sort which u...

Combining two sorted lists in Python

I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a bubble sort or is there a smarter way to do this in Python? ...

Code golf: combining multiple sorted lists into a single sorted list

Implement an algorithm to merge an arbitrary number of sorted lists into one sorted list. The aim is to create the smallest working programme, in whatever language you like. For example: input: ((1, 4, 7), (2, 5, 8), (3, 6, 9)) output: (1, 2, 3, 4, 5, 6, 7, 8, 9) input: ((1, 10), (), (2, 5, 6, 7)) output: (1, 2, 5, 6, 7, 10) Note:...

Sorting an array of arrays by the child array's length?

What's the best way in PHP to sort an array of arrays based on array length? e.g. $parent[0] = array(0, 0, 0); $parent[2] = array("foo", "bar", "b", "a", "z"); $parent[1] = array(4, 2); $sorted = sort_by_length($parent) $sorted[0] = array(4, 2); $sorted[1] = array(0, 0, 0); $sorted[2] = array("foo", "bar", "b", "a", "z"); ...

Sorting values, but only if they are X more than current order

Hi, I've been searching Google (and stack overflow of course!) for a way to sort a list of integers by value, but also by an extra factor. I'm looking for some sort of algorithm to implement I suppose. So far, I have an array in Delphi 2007 that sorts the values from largest to smallest, but now I would like it to sort values that are...

Is there an existing delegate in the .NET Framework for comparison?

The .NET framework provides a few handy general-use delegates for common tasks, such as Predicate<T> and EventHandler<T>. Is there a built-in delegate for the equivalent of CompareTo()? The signature might be something like this: delegate int Comparison<T>(T x, T y); This is to implement sorting in such a way that I can provide a la...

How to Sort Data Table like FogBugz Cases Table

Anyone ever see how fogbugz sorts their tables? When you click to sort the column, they actually break the table up into many small tables that have each category of info. Wondering if anyone knows how they do this? Looking to implement this feature. If you take a look through the cases page, and sort you can see what I mean. Any he...

Let user sort records ?

Hello, i am trying to figure out a way to let the user sort records (etc a list of friends). I want to give the user the opportunity to move a record (friend) straight to the top or bottom of the list, or by entering a number (in between). First i thought of just adding a column called SortOrder (int) to the table with all the users f...

How do you remove duplicates from a list in Python?

Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this: from sets import Set [...] myHash = Set(myList) but I don't know how to retrieve the list members from the hash in alphabetical order. I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an...

Accessing object properties from string representations

I've got a custom object (example only code for ease of understanding) ... public class MyClass { private string name; private int increment; private Guid id; public string Name { get { return name; } set { name = value; } } public int Increment { get { return increment; } ...

Why doesn't java.lang.Number implement Comparable?

Does anyone know why java.lang.Number does not implement Comparable? This means that you cannot sort Numbers with Collections.sort which seems to me a little strange. Post discussion update: Thanks for all the helpful responses. I ended up doing some more research about this topic. The simplest explanation for why java.lang.Number do...

What is the best way to sort a partially ordered list?

Probably best illustrated with a small example. Given the relations A < B < C A < P < Q Correct outputs would be ABCPQ or APQBC or APBCQ ... etc. In other words, any ordering is valid in which the given relationships hold. I am most interested in the solution that is easiest to implement, but the best O(n) in speed and time is int...

SQL UNION and ORDER BY

I have an annoying SQL statement that seem simple but it looks awfull. I want the sql to return a resultset with userdata ordered so that a certain user is the first row in the resultset if that users emailaddress is in the companies table. I have this SQL that returns what i want but i think it looks awful: select 1 as o, * from User...

Keeping an array sorted in PHP

I have a PHP script which reads a large CSV and performs certain actions, but only if the "username" field is unique. The CSV is used in more than one script, so changing the input from the CSV to only contain unique usernames is not an option. The very basic program flow (which I'm wondering about) goes like this: $allUsernames = arra...

XML sorting

I have the following XML <fields> <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" /> <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" /> <field position="3" tablename="Students" headername="Race" field...

will Array.sort() preserve the order of the array, where possible?

I suppose it depends on how it's implemented. I'd love it if someone would come back and tell me "yes, in virtually all browsers, order of items will be changed only when necessary to satisfy the conditions of the sort. ...

How to sort a listview by file size?

I have a listview with 4 columns - Name, Extension, Size and Location. I have a method that takes the file size in bytes and converts to KB, MB, GB, etc as needed. An example output would be a 1024 byte file which is printed as "1KB". This value is then placed in the listview. What I need to do is sort the size column intelligently. Rig...

Do the items in an array or list maintain their order?

I am coding VB.NET in VS2008. I have a comma delimited string of numbers, i.e. 16,7,99,1456,1,3 I do this in VB: Dim MyArr() As String = MyString.Split(",") Will MyArr keep the items in the order they were in the string? If I do this: For Each S as String in MyString.Split(",") 'Do something with S 'Will my items be in the...