Javascript Fast Sorting a 2 dimensional array

Related searches

So I'm trying to implement a sort of data table view with in React Native. All I really need to do is sort the columns. The data structure I am trying to sort is sorta like this:

[
   [
     element,
     element,
     (9 elements per array)
   ],
   [
     element,
     element,
     (9 elements per array)
   ]
]

where I have a total of 170+ arrays in the base array and each child array has 9 elements.

I've been able to accomplish the sorting using essentially this method:

return items.sort(function(a,b) {
   return a[col].value > b[col].value ? 1:-1
})

The problem I run into is that the sort takes a good second or two. I don't notice this delay with web data tables so I'm wondering if there is some sort of sorting algorithm I should be using for a faster sort? Should I possibly be structuring my data differently (though I was having similar results by having an array of objects)?

You can shorten your sort function to

items.sort(function(a,b) {
   return a[col].value - b[col].value;
})

but even then, you probably won't see much improvements in speed. Memoization may help with successive calls if you don't expect your data to change often. Will all 170 elements be visible from the same screen? If not, you may consider using lazy loading (sort the first 30 elements, display results and then consider the rest of your data).

JavaScript Array sort: Sorting an Array More Effectively, The sort() method casts elements to strings and compares the strings to determine the orders. Consider the following example: let numbers = [0, 1 , 2� Sorting a two dimensional array in numerical order To sort an array in numerical order, simply pass a custom sortfunction into array.sort() that returns the difference between “a” and “b”, the two parameters indirectly/automatically fed into the function:

Sorting 170 items using Javascript's Array.prototype.sort() shouldn't take more than a few ms-s so it is likely to be a rendering issue.

It's a good practice to separate logic like this sorting from rendering so that it can be unit tested and measured.

JavaScript, JavaScript Sorting multidimensional array. Example#. Given the following array var array = [ ["key1", 10], ["key2", 3], ["key3", 40], ["key4", 20] ];. You can sort it sort � The sort() method accepts an optional argument which is a function that compares two elements of the array.. If you omit the compare function, the sort() method sorts the elements with the sort order based on the Unicode code point values of elements as mentioned earlier.

You can eliminate the ternary operation - just use subtraction:

return a[col].value - b[col].value;

If they're strings, not numbers:

return a[col].value.localeCompare(b[col].value);

Row wise sorting in 2D array, Start iterating through each row of given 2D array, and sort elements of each row using efficient sorting algorithm. Java; Python3; C#. Java. The two-dimensional array is a collection of items which share a common name and they are organized as a matrix in the form of rows and columns. The two-dimensional array is an array of arrays, so we create an array of one-dimensional array objects. The following program shows how to create an 2D array : Example-1:

sort 2D arrays, How To Sort Two Dimensional Array By Selected Column Index In It is easy to manipulate with data in any way and offers fast sorting as well. If we want to use anything which acts as a multidimensional array then we need to create a multidimensional array by using another one-dimensional array. So multidimensional arrays in JavaScript is known as arrays inside another array. We need to put some arrays inside an array, then the total thing is working like a multidimensional array.

Sophisticated Sorting in JavaScript, This is how we sort multi-dimensional arrays, using a value from each inner array as the sort criterion. All the other inner values just 'come� Below are the steps that follow: Step 1: Input the 2D array of ‘m’ rows and ‘n’ columns. Example: Step 2: Print the original array. Step 3: Create a 1D array of size ‘ m*n ‘ Step 4: Save all elements of 2D array into 1D array (i.e. converting a 2D array into a 1D array) Step 5: Sort the 1D array you

There is an overloaded sort method in java.util.Arrays class which takes two arguments: the array to sort and a java.util.Comparator object. You can add the following lines of code with your program to get the expected result.

Comments
  • If value is a number, use return a[col].value - b[col].value
  • Although I wouldn't expect this to take so long with your method.
  • The size of the child arrays shouldn't affect the sorting time.
  • Can the values ever be equal? You're supposed to return 0 when they are.
  • @Barmar thanks, the equals return 0 may have helped a little. What sped up the operation significantly was just rendering the first 20 rows. The underlying data is still there (and getting sorted) but React doesn't have to re-render as much once sorted... which unfortunately leads me to think that its a react rendering issue rather than the sorting itself (sigh)
  • If you don't sort the whole array, how do you know which are the first 30?
  • This can be handled on the backend, have it send 9 additional array of arrays of entries sorted by lowest values for each column.
  • I don't believe localeCompare works in React Native for whatever reason.