ordering

Force numerical order on a SQL Server 2005 varchar column, containing letters and numbers?

I have a column containing the strings 'Operator (1)' and so on until 'Operator (600)' so far. I want to get them numerically ordered and I've come up with select colname from table order by cast(replace(replace(colname,'Operator (',''),')','') as int) which is very very ugly. Better suggestions? ...

Returning query results in predefined order

Is it possible to do a SELECT statement with a predetermined order, ie. selecting IDs 7,2,5,9 and 8 and returning them in that order, based on nothing more than the ID field? Statements SELECT id FROM table WHERE id in (7,2,5,9,8); and SELECT id FROM table WHERE id in (8,2,5,9,7); both return them in the same order. ...

Ordering numbers that are stored as strings in the database

I have a bunch of records in several tables in a database that have a "process number" field, that's basically a number, but I have to store it as a string both because of some legacy data that has stuff like "89a" as a number and some numbering system that requires that process numbers be represented as number/year. The problem arises ...

ordering by collection count in nhibernate

Is there a way of ordering a list of objects by a count of a property which is a collection? For arguments sake let's say I have a question object with a question name property, a property that is a collection of answer objects and another property that is a collection of user objects. The users join the question table via foreign key ...

Best way to save a ordered List to the Database while keeping the ordering

Hi, I was wondering if anyone has a good solution to a problem I've encountered numerous times during the last years. I have a shopping cart and my customer explicitly requests that it's order is significant. So I need to persist the order to the DB. The obvious way would be to simply insert some OrderField where I would assign the n...

Visually order large data sets

I have an interesting problem: I'm building a web based image gallery program that right off the bat is going to include almost 7,000 images. So, here's my question: What would be an optimal way to build an ordering system? For example, if the admin wants to have the 4,984 image land in the 3,223 position (I work with some anal peopl...

How can you compare to what extent two lists are in the same order?

I have two arrays containing the same elements, but in different orders, and I want to know the extent to which their orders differ. The method I tried, didn't work. it was as follows: For each list I built a matrix which recorded for each pair of elements whether they were above or below each other in the list. I then calculated a pea...

Ordering by the order of values in a SQL IN() clause

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause. The problem is that I have 2 queries, one that gets all of the IDs and the second that retrieves all the information. The first creates the order of the IDs which I want the second to order by. The IDs are put in an IN() clause...

Ordering admin.ModelAdmin objects in Django Admin

Let's say I have my pizza application with Topping and Pizza classes and they show in Django Admin like this: PizzaApp - Toppings >>>>>>>>>> Add / Change Pizzas >>>>>>>>>> Add / Change But I want them like this: PizzaApp - Pizzas >>>>>>>>>> Add / Change Toppings >>>>>>>>>> Add / Change ...

Optimising if statements by reordering

I remember reading once that the order of the members of an evaluation is important. Such as if (null == myClass) is better (faster?) then if (myClass == null) is this the case? If so could someone explain how and why? If the answer requires a language then aim towards c#. Thanks ...

List ordering from SQL call

i have a List being returned from a daoTemplate.query() call, using a rowMapper. the sql does an order by on a column, happens to be a date column, order by asc when i use the List, am i always guaranteed that the list will be in the order dictated by the sql. can i depend that the first element (theList.get(0)), will ALWAYS be the earli...

Reordering of controls within a flow layout panel

Good afternoon, I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically have is a flow layout panel that has 3 sections. Section #1 is a set of 2 controls .. two dropdown controls, they are always in the same order, always visible in all instances Section #2 is a set of 5 different controls ... based...

Adding ORDER BY clause to MySQL query makes it return in ~30 seconds, up from ~0.5

So I have this query that is relatively fast at ~0.5 seconds but when I add an ORDER BY clause it jumps up to nearly 30 seconds. Original query: (returns in ~0.5 seconds) SELECT table1.*,table2.* FROM table1 LEFT OUTER JOIN table2 ON table1.column2=table2.column3 WHERE table1.column1='value' LIMIT 4 Query with ORDER BY: (returns in ~...

Django: QuerySet order by method

Hi! Let's say I have the following model: class Contest: title = models.CharField( max_length = 200 ) description = models.TextField() class Image: title = models.CharField( max_length = 200 ) description = models.TextField() contest = models.ForeignKey( Contest ) user = models.ForeignKey( User ) def score...

ORM or something to handle SQL tables with an order column efficiently

Hi, I got an Java application using SQL tables that contains an ordered list of entities, ordered by an order column. I would like to add/remove things in/from the middle of the list. Now, I'm wondering if some persistence framework / orm / you-name-it could provide this kind of functionality with batch update of order column. At the...

How to save a particular, mutable "order" into a Database

Suppose I have some objects, and I want the user to be able to reorder them in any way they wish, say, by dragging them around. So I'd have cheese muffins milk and then the user drags 'milk' to the top, making the new order milk cheese muffins Is there a best practice how to store the order of these objects in a database? The nai...

Alphabetically Sort a Java Collection based upon the 'toString' value of its member items.

Assume I have a user defined Java class called Foo such as: public class Foo { private String aField; @Override public String toString() { return aField; } } and a Collection such as: List<Foo> aList; What I am looking to do is to sort the List alphabetically based upon each member's returned '.toString(...

CAML Query not ordering properly

Can anyone help me with this CAML query? When I flip the Ascending attribute from TRUE to FALSE (have also tried True and False), it doesn't re-order the result set. The rest of the CAML is correct, it is being generated by a tool and the appropriate results are being returned. <Where> <And> <And> <Eq> <FieldRef ...

python: arbitrary order by

In Oracle sql there is a feature to order as follow: order by decode("carrot" = 2 ,"banana" = 1 ,"apple" = 3) What will be the best way to implement this in python? I want to be able to order a dict on its keys. And that order isn't alphabetically or anything, I determine the order. ...

selecting the next record from a list ordered by date.

I am using this SQL query to order a list of records by date in a php page. SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME FROM table WHERE upper(ARTICLE_NAME) LIKE % x % ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'); This works fine. In a different php page, I want to be able to delete this record, and show the n...