sorting

Getting the lesser n elements of a list in Python

I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times. n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the function. Sorting can't be mad...

SQL query (order by)

I want to list (a sorted list) all my entries from an attribute called streetNames in my table/relation Customers. eg. I want to achieve the following order: Street_1A Street_1B Street_2A Street_2B Street_12A Street_12B A simple order by streetNames will do a lexical comparision and then Street_12A and B will come before Street_2A/B...

Flex - how do I sort a datagrid column that is using an item renderer in the header?

I am using an advanced data grid that is using a custom item renderer for the column heading and now sorting doesn't work. If I take out the custom renderer it works fine but I need it to work with the renderer. Does anyone know how to do this? I am new to Flex and ActionScript. ...

Most efficient sorting algorithm for many identical keys?

What is the most efficient algorithm for grouping identical items together in an array, given the following: Almost all items are duplicated several times. The items are not necessarily integers or anything else that's similarly simple. The range of the keys is not even well-defined, let alone small. In fact, the keys can be arbitrar...

How to properly sort alphanumeric in SQL Server 2000

MS SQL Server 2000 I have a column in Table A called Name. I wish to sort the Name field. Many but not all of the records for Name start will KL and are followed by a number (KL 1234, KL 2, KL 323, etc). Table A Name Able Bravo KL 2 KL 323 KL 1234 Zebra If I use Select Name from A Order by Name I get Able Bravo KL 1234 KL 2 KL ...

Sorting multiple keys with Unix sort

I have potentially large files that need to be sorted by 1-n keys. Some of these keys might be numeric and some of them might not be. This is a fixed-width columnar file so there are no delimiters. Is there a good way to do this with Unix sort? With one key it is as simple as using '-n'. I have read the man page and searched Google ...

Remove duplicate items with minimal auxillary memory?

What is the most efficient way to remove duplicate items from an array under the constraint that axillary memory usage must be to a minimum, preferably small enough to not even require any heap allocations? Sorting seems like the obvious choice, but this is clearly not asymptotically efficient. Is there a better algorithm that can be d...

SQL Sorting and hyphens

Is there a way to easily sort in SQL Server 2005 while ignoring hyphens in a string field? Currently I have to do a REPLACE(fieldname,'-','') or a function to remove the hyphen in the sort clause. I was hoping there was a flag I could set at the top of the stored procedure or something. Access and the GridView default sorting seems to...

Average time complexity of quicksort vs insertion sort

I'm lead to believe that quick sort should be faster than insertion sort on a medium size unorderd int array. I've implemented both algorithms in java and I notice quicksort is significantly slower then insertion sorrt. I have a theory: quiksort is being slower because it's recursive and the call it's making to it's own method signat...

How to sort not simple hash (hash of hashes)

Hi all! I have a Hash like this { 55 => {:value=>61, :rating=>-147}, 89 => {:value=>72, :rating=>-175}, 78 => {:value=>64, :rating=>-155}, 84 => {:value=>90, :rating=>-220}, 95 => {:value=>39, :rating=>-92}, 46 => {:value=>97, :rating=>-237}, 52 => {:value=>73, :rating=>-177}, 64 => {:value=>69, :rating=>-167}, 86 => {:v...

Is it possible to sort numbers in a QTreeWidget column??

I have a QTreeWidget with a column filled with some numbers, how can I sort them? If I use setSortingEnabled(true); I can sort correctly only strings, so my column is sorted: 1 10 100 2 20 200 but this is not the thing I want! Suggestions? ...

xsl grouping sort problem

Hi I have the following xsl template that I'm using to group my xsl. The problem I have is that I need to uppercase the @Title as currently my grouping is seeing upper and lowercase as seperate groups. <xsl:key name="rows-by-title" match="Row" use="substring(@Title,1,1)" /> <xsl:template name="Meunchian" match="/dsQueryResponse/...

Sorting IQueryable by Aggregate in VB.net

Hello all, been searching for a quick example of sorting a IQueryable (Using Linq To SQL) using a Aggregate value. I basically need to calculate a few derived values (Percentage difference between two values etc) and sort the results by this. i.e. return rows.OrderBy(Function(s) CalcValue(s.Visitors, s.Clicks)) I want to call an exte...

How to automatically sort a QTreeWidget column?

Hello, i'm using a QTreeWidget to display some simple items. I've set the list sortable by .setSortingEnabled(true) calling. In this way, the list is sorted only when the user press the title column, and not automatically whenever new item is inserted. I have two question: Is there a way to force the automatic sorting in a specified co...

What's the efficiency and quality of this shuffling algorithm?

This recent question about sorting randomly using C# got me thinking about the way I've sometimes shuffled my arrays in Perl. @shuffled = sort { rand() <=> rand() } @array; The proposed solution in the mentioned question is Fisher-Yates shuffle, which works in a linear time. The question is: how efficient is my snippet and is such sh...

Formatting Tables to not look like tables / JS Sorting without tables?

I'm designing an interface to display sets of data. I'm interested in organizing the information in a customized format using DIVs and SPANs to identify where specific data will be (not in the typical row-by-row format tables provide) but still having the flexibility of sorting and organizing the data that most JS libraries provide for t...

String "Sort Template" in C#

I'm trying to come up with a clean way of sorting a set of strings based on a "sorting template". I apologize if my wording is confusing, but I can't think of a better way to describe it (maybe someone can come up with a better way to describe it after reading what I'm trying to do?). Consider the following list of strings (my "sort tem...

How can I sort an SQL result but keep some results as special?

I have a result from an SQL query that I would like to sort alphabetical, but there are a couple of commonly used results that I would like to float to the top. Is there a simple way I can achieve this either by doing clever SQL or by sorting the (asp.net) datatable once I have it filled? I know the database ID of the things that I wan...

How can I sort a hash's keys naturally?

I have a Perl hash whose keys start with, or are, numbers. If I use, foreach my $key (sort keys %hash) { print $hash{$key} . "\n"; } the list might come out as, 0 0001 1000 203 23 Instead of 0 0001 23 203 1000 ...

When will a Comparer make Sort throw an ArgumentException?

The documentation for Sort says that Sort will throw an ArgumentException if "The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself." Apart from the example given, can anyone tell me when this would otherwise happen? ...