sorting

Javascript - sorting arrays containing positive and negative "decimal" numbers

I originally had the following callback passed as a parameter to the javascript array sort() function: function sortNumber(a,b) { return a-b; } However this doesn't work when my array contains positive and negative decimal numbers (i.e. -107.578, 97.453 etc.) How would I modify this to sort properly? ...

c++ Sorting a vector based on values of other vector, or what's faster?

Hi, There are a couple of other posts about sorting a vector A based on values in another vector B. Most of the other answers tell to create a struct or a class to combine the values into one object and use std::sort. Though I'm curious about the performance of such solutions as I need to optimize code which implements bubble sort to ...

How does this quicksort algo work?

private static char[] quicksort (char[] array , int left , int right) { if (left < right) { int p = partition(array , left, right); quicksort(array, left, p − 1 ); quicksort(array, p + 1 , right); } for (char i : array) System.out.print(i + ” ”); System.out.println(); return array; } privat...

Space requirements of a merge-sort

I'm trying to understand the space requirements for a Mergesort, O(n). I see that time requirements are basically, amount of levels(logn) * merge(n) so that makes (n log n). Now, we are still allocating n per level, in 2 different arrays, left and right. I do understand that the key here is that when the recursive functions return the sp...

Change C# sorting behaviour

Lets say i have the strings abc_ and abc2_. Now, normally when sorting i C# abc2_ would come after abc_, leaving the result: abc_ abc2_ I am using this to sort, if it matters: var element = from c in elements orderby c.elementName ascending select c; How can i change this? I want abc_ to come last. Reversing is not ...

Django: ordering numerical value with order_by

I'm in a situation where I must output a quite large list of objects by a CharField used to store street addresses. My problem is, that obviously the data is ordered by ASCII codes since it's a Charfield, with the predictable results .. it sort the numbers like this; 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21.... Now the ob...

Gridview Edit not working after Sorting ASP.NET

Hi All, I am using C#,ASP.NET I have a Gridview for which I have provided Sorting, Edit functionality. I am not able to perform EDIT when I perform Sorting. After sorting edit is set on some other row. I think there is some problem with the index it is taking.. Can any one help me how to fix this.. Regards sbmarya ...

What sorting method to use: quicksort, bucket sort, radix, ... for tiny data pairs? (c++)

Hi, I need to optimize some code that sorts a vector<pair<int, float >>a where the pairs needs to be sorted on the float value. The vector will have an length between 0 and 5. I've been googling and reading up on sorting methods in C++ but cannot find any benchmarks on sorting tiny data sets. For the system it's important be as fast as ...

how to sort a scala.collection.Map[java.lang.String, Int] by its values?

How would you sort a scala.collection.Map[java.lang.String, Int] by its values (so on the Int)? What is a short and elegant way to do that? ...

An algorithm for sorting & grouping a list of weighted objects

I have a number of chunks of data. For arguments sake we'll say they are File 1 - 150Kb File 2 - 50Kb File 3 - 70Kb File 4 - 60Kb File 5 - 70Kb File 6 - 100Kb File 7 - 90Kb For transmission, I can package these up into a max payload of 300Kb. If you just iterate through them in order you'd get TRANS1: 150Kb + 50Kb + 70Kb = 270...

How to identify orphaned nodes

I have a hierarchy of nodes stored in DB. I select all, store them in an array, then iterate over them and create a nested array in memory. The input looks like this: [{name: A}, {name: B}, {name: X, parent: A}, {name: Y, parent: A}, {name: C}] The output looks like this: [{name: A, children:[{name: X}, {name: Y}]}, {B}, {C}] ...

How to sort an array of arrays in php?

In php I have a numerical array of associative arrays: mainArray: [ array1:['title':'Record a','order':'2'], array2:['title':'Record b','order':'4'], array3:['title':'Record c','order':'1'], array4:['title':'Record d','order':'3'] ] What is the simplest way to sort mainArray by the 'order' value of each associative array? Th...

How can I use STL sort in c++ to sort some values in the class?

I have a class named Graph, in this class I have a member named V, it is a vector. I have a struct named Edge, and a list of Edges. like below: struct Edge{ int u; int v; Edge(int u,int v){ this->u=u; this->v=v; } }; struct Vertex{ int d; int f; . . . } class Graph{ vector < Vertex > V; . . . int edgeCmp(Edge* ...

Order by text and then by number

I have data like: Audio 1 Test File 10 Audio 2 Audio 3 File 11 Audio 1 Audio 13 Audio 22 File 20 Test Test File 22 Audio 10 File 1 File 2 I need it order first by the text (i.e. Audio, File, Test) and then by number (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 etc.) The problem is that sorti...

Broken sorting by values from embedded documents in MongoDB?

I am trying to sort the documents which I retrieve from MongoDB by a key that is in the first of several embedded documents. I am using mongod v1.4.3 with the ruby gem mongo-1.0.2. One document looks like this: >> pp MONGODB.collection('hotel_offer_groups').find_one "offers"=> [{"price"=>520.0, "rate"=>nil, "id"=>523960640,...

Please help - sorting integers in structs

I have a struct like this: struct db { string name,sur; int num; }; And declared an array of db structs: struct db a[10]; and every member of a[] is filled with a name, surname and a number but there can be the same number appearing multiple times. I need to sort the numbers, and print the results, i.e. sort by the num of each s...

how to implement a really efficient bitvector sorting in python

Hello guys! Actually this is an interesting topic from programming pearls, sorting 10 digits telephone numbers in a limited memory with an efficient algorithm. You can find the whole story here What I am interested in is just how fast the implementation could be in python. I have done a naive implementation with the module bitvector. T...

Sorting arrays in Java

Write a static method in Java: public static void sortByFour (int[] arr) That receives as a parameter an array full of non-negative numbers (zero or positive) and sorts the array in the following way: In the beginning of the array all the numbers that are divisible by four will appear. After them all the numbers in the array that di...

Specific complex array sorting

Okay, a before; Array ( 'home' => array('order' => 1), 'about' => array(), 'folio' => array('order' => 2), 'folio/web' => array('order' => 2), 'folio/print' => array('order' => 1) 'contact' => array('order' => 2) ) And a desired after; Array ( 'home' => array('order' => 1), 'contact' => array('order' =...

Efficient way to maintain a sorted list of access counts in Python

Let's say I have a list of objects. (All together now: "I have a list of objects.") In the web application I'm writing, each time a request comes in, I pick out up to one of these objects according to unspecified criteria and use it to handle the request. Basically like this: def handle_request(req): for h in handlers: if h....