0
votes

I'm not sure if this has happened as a result of upgrading to the new console or if it has always happened, but I just found a bug in my code where the numbers get ordered like this:

1, 12, 2, 21, 3, 33, 4

so if I were to do something like this:

ref.orderByKey().startAt(1).endAt(20).on("value", function);
// Returns: 1, 12, 2
// Desired result: 1, 2, 3, 4, 12

Note that I have used small numbers for readability, I am actually using date strings like 2016-4-13 which is why they are strings.

Is there a solution to this or am I going to have to decide to sacrifice querying or key readability?


More info: The data is getting a set of dates from a large list of dates. The only reason for the startAt and endAt is to limit the amount the user as to download. For example, to get a week from a certain time.

Code Example

ref.orderByKey().startAt("2016-4-5").endAt("2016-4-11").on("value", function(snapshot){
   //returns null
 });

Data

  {
  "GroupOne" : {
    "2015-11-1" : {
      "simplelogin:75" : 5.4,
      "simplelogin:77" : 5.5,
      "simplelogin:80" : 5.5
    },
    "2015-11-2" : {
      "simplelogin:75" : 5.4,
      "simplelogin:77" : 5.6,
      "simplelogin:80" : 5.9
    },
    "2015-11-22" : {
      "simplelogin:24" : 2.1
    },
    "2015-11-3" : {
      "simplelogin:77" : 3,
      "simplelogin:80" : 0.2
    },
    "2015-11-31" : {
      "simplelogin:77" : 15.8
    },
    "2015-11-4" : {
      "simplelogin:77" : 5.1,
      "simplelogin:80" : 0.3
    },
    "2015-11-5" : {
      "simplelogin:77" : 3.8
    },
    "2015-11-6" : {
      "simplelogin:75" : 0.1
    },
    "2016-0-1" : {
      "simplelogin:77" : 13.1
    },
    "2016-0-10" : {
      "simplelogin:75" : 3
    },
    "2016-0-11" : {
      "simplelogin:75" : 3
    },
    "2016-0-12" : {
      "48506d5c-319e-4f47-a29f-c47d6ed82a17" : 3.8,
      "simplelogin:75" : 3.2
    },
    "2016-0-22" : {
      "simplelogin:78" : 1
    },
    "2016-0-5" : {
      "simplelogin:80" : 3
    },
    "2016-0-8" : {
      "simplelogin:24" : 0.9,
      "simplelogin:77" : 3.2
    },
    "2016-1-23" : {
      "simplelogin:75" : 3.1
    },
    "2016-1-27" : {
      "simplelogin:75" : 6.3
    },
    "2016-1-28" : {
      "simplelogin:75" : 12.2
    },
    "2016-2-15" : {
      "simplelogin:75" : 1
    },
    "2016-2-2" : {
      "simplelogin:75" : 5.1
    },
    "2016-2-22" : {
      "simplelogin:75" : 1,
      "simplelogin:78" : 1,
      "simplelogin:80" : 1
    },
    "2016-2-23" : {
      "simplelogin:75" : 4.3
    },
    "2016-2-24" : {
      "simplelogin:75" : 5.7
    },
    "2016-2-25" : {
      "simplelogin:75" : 1
    },
    "2016-2-26" : {
      "simplelogin:75" : 2
    },
    "2016-2-27" : {
      "simplelogin:75" : 1
    },
    "2016-2-28" : {
      "simplelogin:75" : 1
    },
    "2016-2-30" : {
      "simplelogin:75" : 6
    },
    "2016-3-1" : {
      "simplelogin:75" : 2,
      "simplelogin:77" : 2
    },
    "2016-3-11" : {
      "simplelogin:75" : 8.6,
      "simplelogin:80" : 0.9
    },
    "2016-3-12" : {
      "simplelogin:75" : 1
    },
    "2016-3-14" : {
      "simplelogin:75" : 3.8
    },
    "2016-3-6" : {
      "simplelogin:77" : 13.2
    },
    "2016-4-10" : {
      "simplelogin:75" : 2
    },
    "2016-4-15" : {
      "simplelogin:75" : 1
    },
    "2016-4-25" : {
      "simplelogin:75" : 1
    },
    "2016-4-5" : {
      "simplelogin:75" : 6
    }
  }
}
2
You are sorting strings... use Date.parse("2016-4-13") or pass the whatever argument dynamically holds that data string. - Redu
The code that you includes returns the correct numbers. But you're not showing how you process them, which means it's hard to say what's going wrong. Also: when talking about data stored in Firebase Database, always include a minimal snippet of the JSON to show us what data you're working with. You can easily get the JSON (as text, no screenshot please) by clicking the export button in the Firebase Console. - Frank van Puffelen
I added some more info, but I think the current best answer was the solution, in adding zeros in front of the smaller numbers. - Marty.H

2 Answers

1
votes

Since the strings are rigidly formatted, alpha sorting will do the right thing for you, assuming it's yyyy-mm-dd, so the significance of the fields is in the proper order.

0
votes

JavaScript sorting does not work in numerical order. It works more in alphabetical order.

To sort by value, you would have to pass in parameters

var result = document.getElementById("result");

var numbers = [1, 12, 2, 21, 3, 33, 4];

var sorted = numbers.filter(function(number) {
    return number >= 1 && number <= 20;
}).sort(function(a,b) { return a - b });

result.innerHTML = sorted;
<p id="result"></p>

Here is the documentation on Array.prototype.sort

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

It looks like Firebase's orderbyKey() method has this type of implementation. That would explain the order.

When using orderByKey() to sort your data, data will be returned in ascending order by key name as follows. Keep in mind that keys can only be strings.

https://www.firebase.com/docs/web/guide/retrieving-data.html#section-ordered-data

I can't find the source code for this function, so I would recommended not using it at all and using my method.