0

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)?

5
  • If value is a number, use return a[col].value - b[col].value Commented May 16, 2019 at 21:34
  • Although I wouldn't expect this to take so long with your method. Commented May 16, 2019 at 21:35
  • The size of the child arrays shouldn't affect the sorting time. Commented May 16, 2019 at 21:35
  • Can the values ever be equal? You're supposed to return 0 when they are. Commented May 16, 2019 at 21:36
  • 1
    @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) Commented May 16, 2019 at 21:45

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
1

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.

Comments

0

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);

1 Comment

I don't believe localeCompare works in React Native for whatever reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.