tmptplayer
tmptplayer

Reputation: 509

Sorting Imported Records from JSON Document

I have an object which is imported from a JSON document, which is organized like so:

[{
  'Surname': 'Surname',
  'FirstName': 'FirstName'
}
(...)
]

I read the file, run JSON.Parse, and have an object which integrates well with other functions in my application. My issue is sorting this list by names.

I try the following code, and my logger on the first line of the compare function is noting that both arguments are undefined. What am I doing wrong?

const pList = dataService.getLocalSaveData();
pList.sort(sortingUtil.compareByName());

When I iterate through the object like this, all of the data is present and functional:

for (var x of pList) {
    innerList += generateItemFromPerson(x);
  }

Upvotes: 0

Views: 43

Answers (1)

Andrew Furman
Andrew Furman

Reputation: 55

pList.sort(sortingUtil.compareByName());

In this line, you are invoking the function rather than passing it as an argument. I believe what you are looking for is:

pList.sort(sortingUtil.compareByName);

Upvotes: 2

Related Questions