1

I have script that displays a list of employees with their last known location in the warehouse. Unfortunately, it does not sort the names alphabetically; consequently, for a long employee list, it gets messy. Employee Data are stored like this in the js file:

var _Employees = {
"%EmployeeID%" : "%login% |%Name%,%Surname%",
"%EmployeeID%" : "%login% |%Name%,%Surname%",
"%EmployeeID%" : "%login% |%Name%,%Surname%",
"%EmployeeID%" : "%login% |%Name%,%Surname%",
"%EmployeeID%" : "%login% |%Name%,%Surname%",
};

DrawContent Function:

function DrawContent(){
  var aa = '<table id="Results"><tr><th>Surname, Name</th><th>Last Scan</th><th>Last Activity</th><th>Workstation</th></tr>';
  $.each(_Employees, function(_employeeId, _employee){
    var _employeeName = _employee.split('|')[1];
    aa += '<tr class="aa_' + _employeeId + '">';
    aa += '<td>' + _employeeName + '</td>';
    aa += '<td></td>';
    aa += '<td></td>';
    aa += '<td></td>';
    aa += '</tr>';
  });
  aa += '</table>';

  $('#mydiv').append(aa);
}

Could someone assist and provide me a quick and simple solution to list the names in alphabetical order?

Thanks!

David W
  • 10,062
  • 34
  • 60
Cankiee
  • 11
  • 1
  • 4
    Have you checked out the `.sort()` method yet? https://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript – justDan Mar 09 '20 at 19:36
  • 1
    Hi Canikee! You might consider looking through some examples of sorting routines in Javascript - you might find something you can readily adapt to your situation and solve your problem more quickly than someone here. – David W Mar 09 '20 at 19:39

1 Answers1

0

something like:

Object.entries(_Employees) // convert to array of [key, value] pairs
      .sort((e1,e2) => e1[1].localeCompare(e2[1])) // sort by value
      .forEach(([employeeId, employeeName]) => { // for each on the sorted array
        // whatever with sorted array
      })
bryan60
  • 28,215
  • 4
  • 48
  • 65