I have a page that contains a table like the following (automatically sorted by "Name" column)

```
Name Open Num Total Num
-----------------------------------
Doe, John 0 0
Smith, Sarah 4 3
Tyler, Rose 7 8
```

The second `tr`

would look like this:

```
<tr id="1"><td class="N">Smith, Sarah</td><td class="O">4</td><td class="T">3</td></tr>
```

Where the row ID is a counter (first row = 0, second = 1, third = 2, etc.) and the cell class is to grab the column using jQuery (`$(".O")`

gives `Open Num`

column)

I'm trying to get the table to sort based off of the numerical columns (`Open Num`

and `Total Num`

). So output would look like this (sorted by `Open Num`

or `Total Num`

):

```
Name Open Num Total Num
-----------------------------------
Tyler, Rose 7 8
Smith, Sarah 4 3
Doe, John 0 0
```

So far, I store the numbers into an array `arrQuick`

and I store the row number in a different array `rowCount`

. I then use the Quick Sort method to sort the data, at the same time sorting the second array, which works perfectly. So now I have the sorted data and the order that my rows should be in.

**The Problem**

I cannot figure out how to get the table rows to update correctly.

So far I have this.

```
for(var i=0;i<rowCount.length;i++){
var tmpHolder=$("#"+i).html();
$("#"+i).html($("#"+rowCount[rowCount.length-(i+1)]).html());
$("#"+rowCount[rowCount.length-(i+1)]).html(tmpHolder);
}
```

When stepping through I can see that initially the updating is working. However, eventually it gets to some point rows are getting updated to places they shouldn't be and I'm not sure why.

You can sort the rows based on the values of table cells. The following method accepts a className of the cells and sorts the rows based on the text contents of that cells.

```
$.fn.sortTable = function(cls) {
this.find('tr').sort(function(a, b){
return $(a).find('td.'+cls).text() > $(b).find('td.' + cls).text();
}).appendTo(this.find('tbody'));
}
$('table').sortTable('O');
```

Updated method for supporting ascending and descending orders.

```
$.fn.sortTable = function (opt) {
var sort = typeof opt.sortType !== 'undefined' ? opt.sortType : 'asc',
cls = opt.cls;
if (!cls) return;
var $tr = this.find('tbody').children().sort(function(a, b){
return $(a).find('.' + cls).text() > $(b).find('.' + cls).text();
});
if (sort === 'des') {
$tr = $tr.toArray().reverse();
}
this.find('tbody').append($tr);
}
$('table').sortTable({
cls: 'N', // className of the cells
sortType: 'des' // order 'asc' || 'des'
});
```

Similar Questions

Possible Duplicate: Sort arrays of primitive types in descending order Java : How to sort an array of floats in reverse order? How do I reverse an int array in Java? Following code sort the array in

In R, given an numeric array, how to produce another array which contains sorted index order into the original array? To clarify my question, consider an example: For an original array: [33 11 55 22 4

I want to make it so that my multi dimensional array is in a random order. How would you do it? // This is how the array looks like print_r($slides); Array ( [0] => Array ( [id] => 7 [status] =&

There is a sorted array which is of very large size. In it every element is repeated more than once except one element. How can I find the unrepeated element in O(log n) time complextiy.

I'm trying to create a function in order to create a ascending order list by using malloc and realloc. Here is the structure: struct sorted_dyn_array { int *array; int length; int capacity; }; re

Given a sorted array A e.g. {4,9,10,11,19}. The cost for moving from i->j is abs(A[j]-A[i]). Start from a given element e.g. 10. Find out the least cost path without visiting same element twice. So

I have a pretty simple question here: Do red-black trees have to be in sorted order? I ask this because the small box on the right side of the wikipedia page (http://en.wikipedia.org/wiki/Red–black_tr

if an array is given in random order , you have to output the minimum number of swaps required to convert into cyclic sorted array. e.g. array given is 3 5 4 2 1 so the first swap will be 5<-->4

I have a view in backbonejs bound to a sorted collection. I want to periodically update the collection with new models but maintain the sorted order. What is the best way to insert the new models in t

I have a sorted mutable array of a class called Topic. The topics represent a an array of Publications. I present the topics in a table, and periodically fetch new publications from a web service. Whe

I need to display members of community which are sorted by last visit. There are millions of communities each of wich can have millions of members. The list should be scrollable. Because of sorting by

I have an array that is already sorted in partitions of 4: 2, 23, 45, 55, 1, 4, 23, 74545, 75, 234, 323, 9090, 2, 43, 6342, 323452 What would be the most efficient way to sort this array? Note: the a

Can I do a foreach on a function that takes an array object and sorts it. I know I can do a dependent observable, but I have an array of an array coming from the mapping plugin. foreach: sortList($arr

Hello I am trying to make an array that inserts a random number but when it does, the array stays in a maintained order. For example if the array contained 10 20 30 and the random number was 11 the fu

I have to write a C++ code that finds the median and mode of an array. I'm told that it's much easier to find the mode of an array AFTER the numbers have been sorted. I sorted the function but still c

I have the following code because I want to sort an array by the field chat_timestamp: My php code is next, but it seems not working: function compare($a, $b) { return strnatcmp($a[chat_timestamp]

Given an infinite length sorted array having both positive and negative integers. Find an element in it. EDIT All the elements in the array are unique and the array in infinite in right direction. Th

I'm taking a practice exam for a course in programming that deals with MIT Scheme. One of the questions asks: Complete the procedure (in-order ls) to return the list ls, except stop just before the f

In MongoDB, what's the most efficient approach to sort an array in a document and then store the sorted array back to the document? For background, I have a process that generates a collection of time

I need a function that transforms an array of integers in descending order, not allowing any integer in position i to be X times greater than the following in position i+1, by adding 1 or more element

I have 2 sorting alghoritms that provides different results (i sort info by relevancy). As result in both ways I get same items in different order. I know, that first alghorytm provides better results

I have a usort function with a single line: return 0. I tried to use it on an Array of stdClass objects, and it changes their order, how is that possible?

I want to sort a long list, so I put all the elements into a B-tree (each element takes O(log n) time to insert). Once sorted, I need to read back the elements. Do you know how it is done how long it

Does it matter in what order the keys in an array array doing an array_merge, i.e. would the keys in the second array below override the keys in the first array: array1 = array('username' => 'abc',

In python, I am trying to check if a given list of values is currently sorted in increasing order and if there are adjacent duplicates in the list. If there are, the code should return True. I am not

I have an array whose keys are in the format [A1] -> [A20], [B1] -> [B20], etc, and I'm trying to sort that array using first ksort() (to get the letters in the correct order) and then uksort().

I have an array of objects of the following form: arr[0] = { 'item1' : 1234, 'item2' : 'a string' }; I sort it first by 'item1' which is straightforward. Now i want to sort arr (which is sorted by 'i

I am actually trying to solve a problem where I have an array which is sorted but a few numbers are reversed . For example : 1 2 3 4 9 8 7 11 12 14 is the array. Now , my first thought was applying a

Given an array of intergers. how to a program to print all the permutations of the numbers in the array. The output should be sorted in a non-increasing order. For example for the array { 12, 4, 66, 8

I have a mongo query that returns an array in the format below, how do I change it [[12, 1359410400000], [18, 1359410400000]] to look like this [[ 1359410400000, 12], [ 1359410400000,18]]

Given a Sorted Array which can be rotated find an Element in it in minimum Time Complexity. eg : Array contents can be [8, 1, 2, 3, 4, 5]. Assume you search 8 in it.

I have a statement like the below. The order returned is 1,4,5. My code expects 4,5,1 because of output precedence rules. How do i make mysql return the order i specified? select * from Post where fla

here i have wrote code for finding median of two sorted array #include<iostream> using namespace std; #define L 5 #define M 6 const int N=L+M; int A[1000];//define 1 indexed aarray int B[1000];

I have a large dataset I need to sort by a preset order. It's simple enough to get the array sorted, but the problem is having the values not in the sort order moving to the bottom instead of the top.

I've been tasked with creating a method that will print all the indices where value x is found in a sorted array. I understand that if we just scanned through the array from 0 to N (length of array)

I'm trying to have a ComboBox list the contents in sorted order. I've come up with one solution, but it still has some issues. final ObservableList<String> oal = FXCollections.observableArrayLis

For example, to sort an array of complex numbers first by real part, then by magnitude of imaginary part, and then with negative imaginary parts first: def order(a): return a.real, abs(a.imag), sign(a

Hay, i have an array of objects and i need to sort them (either DESC or ASC) by a certain property of each object. Here's the data obj1 = new Object; obj1.date = 1307010000; obj2 = new Object; obj2.da

I am new to C and was writing a function to insert an element to sorted list. But my code does not display the last digit correctly. Though i know there are variety of ways to correct it but i want to

This question already has an answer here: Find the first “missing” number in a sorted list 7 answers I am looking to solve this problem in other than O(n) complexity. Most of the problems alrea

Hi i am having a nsmutable array and in that nearly 50 -60 object having different names ,and can i sort this array in alphabatical order (Is it possible, How?) (is it a duplicate question ?)

Question: what order does the contour from matplotlib expect for the input 2D array? Elaborate: Matplotlib contour documentation says that the routine call is x_axis = np.linspace(10,100,n_x) y_axis

I was asked this question recently in an interview : What is the most efficient way to find a repeated number in a sorted array? My answer was based on using a hash table with key as array element and

I have the the following array of numbers in Ruby (higher is better), and I'd like to rank them. In other words, I want to convert the following sorted list: [89 52 52 36 18 18 5] to the following ra

Consider the data block below, how can I maintain sorted the array by the 3rd field, and keep pushing items? $VAR1 = [ '1111', 'http://...', 3 #this is one of the 3rd field mentioned above ]; $VARN =

This is probably a Microsoft interview question. Find the kth smallest element (ignoring duplicates) out of an sorted array. [EDIT]: The array may contain duplicates(not specified). Thought a bunch of

Answering to another question, I wrote the program below to compare different search methods in a sorted array. Basically I compared two implementations of Interpolation search and one of binary searc

is there a way to sort an array and store the result in a linked list with the original array positions referenced by the list? so 0 bbb 1 aaa 2 ccc would become a linked list 1 aaa 0 bbb 2 ccc Than

I'm refactoring a project that involves passing around a lot of arrays. Currently, each method that returns an array sorts it right before returning it. This isn't ideal for a couple reasons -- there'

While preparing for tech interview I stumbled upon this interesting question: You've been given an array that is sorted and then rotated. example Let arr = [1,2,3,4,5] which is sorted and then rotated