question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JavaScript's .sort() method works in simulator but not on iOS device

See original GitHub issue

I am trying to sort a list of events by date using JavaScript’s built in .sort() method. I am using a ListView to display the data. In dataSource.cloneWithRows() I pass in a function that is supposed to return a sorted array of events. For some reason this works fine in the simulator but doesn’t work on my iOS device. Any help is appreciated!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ademars94commented, Jul 29, 2016

@mkraft If your issue has to do with sorting dates, you’re in luck.

Originally, I was trying to directly compare two JS Date objects here:

// this code exists in a loop, see earlier post
if (new Date(arr[j - 1].startDateTime) > new Date(arr[j].startDateTime)) { 
  //fun stuff in here
}

And since the different runtime environments handle dates differently, it was breaking.

Definitely take a look at the Moment.js library. I am using Moment to make sure the formats stay consistent when comparing dates and when I’m hopping between the dev server and static JS bundle.

So this line:

let aDate = moment(array[j - 1].startDateTime, moment.ISO_8601);

Will give me back the startDateTime (which is a JS Date object), formatted as Moment’s .ISO_8601 format. Now I can compare dates all day long until my fingers fly off.

Here’s my updated block o’ code 4 reference:

sortByDate = (array) => {
    for (let i = array.length - 1; i >= 0; i--) {
      for (let j = 1; j <= i; j++) {
        // Use moment to format dates before they are compared
        let aDate = moment(array[j - 1].startDateTime, moment.ISO_8601);
        let bDate = moment(array[j].startDateTime, moment.ISO_8601);
        if (aDate > bDate) {
          let temp = array[j - 1];
          array[j - 1] = array[j];
          array[j] = temp;
        }
      }
    }
    return array;
  };
0reactions
halaharrcommented, Mar 26, 2018

Hi, I am having same issue. I am trying to sort contacts and show in FlatList. It works in simulator but when I publish the app for testing on devices it does not sort. Here is my sample code. Please let me know if you guys were able to solve this issue.

    phoneContacts.slice.sort((a, b) => {
      const aName = (a.buttonTitle === '+Add' ? '0' : '1') + a.firstName.toLowerCase();
      const bName = (b.buttonTitle === '+Add' ? '0' : '1') + b.firstName.toLowerCase();
      if (aName < b.bName) return -1;
      if (aName > bName) return 1;
      return 0;
    });

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array.sort works in console but not in React Native app
Has anyone seen a time when an Array.sort() would work in some React contexts but not others? javascript · arrays · sorting ·...
Read more >
Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default...
Read more >
[SOLVED] iOS simulators not showing up - Apple Developer
Hello everyone! This problem first appeared when I updated Xcode 6.1 to version 6.2. At that point, the system let me keep 6.1...
Read more >
[Solved]-array not sorting on device - works in Simulator
problem resulted to be in the locale of my iOS device & a different one being used the simulator... One was the same...
Read more >
Changelog - Proxyman
[Diff] Add option to sort Header Key in the Diff View ... Fixed: Install certificate to iOS Simulators with simctl does not work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found