0

my json looks like this:

[
  {
    "cash": 100,
    "uid": "LHy2qRGaf3nkWQgU4axO",
    "name": "test2"
  },
  {
    "cash": 1000000,
    "uid": "01wFhCSlnd9vSDY4NIkx",
    "name": "test"
  },
  {
    "cash": 500,
    "uid": "PBOhla0jPwI4PIeNmmPg",
    "name": "test3"
  }
]

I'm trying to sort the json by the user cash. So what I did is :

    var objArr = []; // the json array

    function compare(a, b) {
        console.log("a" + a.cash);
        console.log("b" + b.cash);
        if (a.cash > b.cash)
            return -1;
        if (a.cash < b.cash)
            return 1;
        return 0;
    }

   var obj = objArr.sort(compare);
   response.send(obj);

but the response that came back is not ordered.

how can I fix it? thanks

ULAQ
  • 41
  • 1
  • 1
  • 4
  • actually it is sorting descending. do you need an ascending sorting? – Nina Scholz Dec 30 '17 at 15:19
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Dec 30 '17 at 15:20
  • @NinaScholz It doesn't matter. the response is not sorted at all – ULAQ Dec 30 '17 at 15:21
  • maybe it's a timing problem, you sort the array before you have the data to sort. – Nina Scholz Dec 30 '17 at 15:37

1 Answers1

1

A couple of notes:

  1. Your code is sorting in descending order (it's not unordered).

  2. Your code is using the return value of sort. Just beware that that gives the misleading impression that the returned array is not the same array as the original. It is the same array, so generally best not to use the return value.

  3. Your sort can be much shorter:

    // Ascending:
    return a.cash - bcash;
    
    // Or descending:
    return b.cash - a.cash;
    

Proof of #1 above:

var objArr = [
  {
    "cash": 100,
    "uid": "LHy2qRGaf3nkWQgU4axO",
    "name": "test2"
  },
  {
    "cash": 1000000,
    "uid": "01wFhCSlnd9vSDY4NIkx",
    "name": "test"
  },
  {
    "cash": 500,
    "uid": "PBOhla0jPwI4PIeNmmPg",
    "name": "test3"
  }
];

function compare(a, b) {
  console.log("a" + a.cash);
  console.log("b" + b.cash);
  if (a.cash > b.cash)
    return -1;
  if (a.cash < b.cash)
    return 1;
  return 0;
}

var obj = objArr.sort(compare);
console.log(obj);
.as-console-wrapper {
  max-height: 100% !important;
}

Demo of #2 and #3:

var objArr = [
  {
    "cash": 100,
    "uid": "LHy2qRGaf3nkWQgU4axO",
    "name": "test2"
  },
  {
    "cash": 1000000,
    "uid": "01wFhCSlnd9vSDY4NIkx",
    "name": "test"
  },
  {
    "cash": 500,
    "uid": "PBOhla0jPwI4PIeNmmPg",
    "name": "test3"
  }
];

console.log("ascending", objArr.sort((a, b) => a.cash - b.cash));
console.log("descending", objArr.sort((a, b) => b.cash - a.cash))
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks, I see that it works here. also, the code I post in question works here. the problem is with the response still come unsorted – ULAQ Dec 30 '17 at 15:27
  • @ULAQ: What is `response.send`? – T.J. Crowder Dec 30 '17 at 15:30
  • @ULAQ: "Yes"? That comment has nothing to do with my question above. – T.J. Crowder Dec 30 '17 at 15:32
  • @ULAQ: Again, that has nothing whatsoever to do with my question above. If you won't answer the questions people ask you, we can't help you. – T.J. Crowder Dec 30 '17 at 15:35
  • response.send is how i sent the request response inside my function `exports.getTopFive = functions.https.onRequest((request, response) => { response.send(obj); });` – ULAQ Dec 30 '17 at 15:40