0

I'm setting up multiple lists with 2 values. Each value is important for sorting. I have added a Switch Button to change the sort logic for every list element.

How to sort each list separately when the ngchange function is triggered. I have no problems to sort arrays. But I didn't understand the sort logic of 2 variables in a string list.

HTML

  <md-switch ng-model="data" ng-change="onChange(data)">
      Sortierung: {{ message }}
    </md-switch>

JS

var list = {
  "languages": {
    "deutsch": 542,
    "französisch": 233,
    "englisch": 64,
    "italienisch": 43,
  },
  "keywordFacets": {
    "Kunstschutz Frankreich": 52,
    "Archivschutz": 20,
    "Dépôts in Frankreich": 17,
    "Bibliotheksschutz": 17,

  },
  "typFacets": {
    "Akte": 619,
    "Unterbestand / Aktengruppe": 97,
    "Bestand": 22
  },
  "locationTypes": {
    "Versailles": 5,
    "Montecassino": 3,
    "Marburg": 3,
    "Aachen": 3,

  },
  "doctypFacets": {
    "archivdokument": 738,
    "person": 12,
    "archiv": 10
  }
}


if (cbState == true) {

    var unordered = list.doctypFacets
    const ordered = {};
    Object.keys(unordered).sort().forEach(function (key) {
        ordered[key] = unordered[key];
    });
    list.doctypFacets = ordered;
    $scope.message = 'A-Z';
} else {
    var unordered = list
    const ordered = {};
    Object.keys(unordered).sort().forEach(function (key, count) {
        ordered[key, count] = unordered[key, count];
    });
    list.doctypFacets = ordered;
    $scope.message = 'Count';
}




This is what I have so far. The Count sort doesn't work.

The result should be:

COUNT

var list = {
  "languages": {
    "deutsch": 542,
    "französisch": 233,
    "englisch": 64,
    "italienisch": 43,
  },
  "keywordFacets": {
    "Kunstschutz Frankreich": 52,
    "Archivschutz": 20,
    "Dépôts in Frankreich": 17,
    "Bibliotheksschutz": 17,
  ....
}

A-Z

var list = {
  "languages": {
    "deutsch": 542,
    "englisch": 64,
    "französisch": 233,
    "italienisch": 43,
  },
  "keywordFacets": {
  "Archivschutz": 20,
  "Bibliotheksschutz": 17,
  "Dépôts in Frankreich": 17,
  "Kunstschutz Frankreich": 52,


  ....
}
Fonty
  • 125
  • 11
  • Possible duplicate of [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – AZ_ Aug 28 '19 at 09:48
  • There's no JSON in your post. JSON is a text format. What you have is called object literal. You can't sort it directly because there's no index; you have to turn it into an array first. – Chris G Aug 28 '19 at 09:50
  • I have updated my question. – Fonty Aug 28 '19 at 11:03

0 Answers0