-1

My JavaScript object needs to be sorted in a reverse fashion based on id's.

Here is my JavaScript object:

var model.todos = 
{
  1:{"id":1,"text":"ksjdv;lv","completed":false},
  2:{"id":2,"text":"lsakckcv","completed":false},
  3:{"id":3,"text":"sakvalvs","completed":false}
}

When I try to sort as specified below I get "uncaught type error undefined is not a function" error

sortId.addEventListener('click',function(){
    todos.sort(sortItems);<================//get uncaught type error undefined is not a function error
    redrawUI(model);
}, false);

function sortItems(a, b) { 
    return( b.id - a.id);
}           

any pointers

user229044
  • 232,980
  • 40
  • 330
  • 338
user3873833
  • 45
  • 1
  • 4

3 Answers3

0

Well, first of all, a javascript object is a dictionary; I'm not sure it's a good idea to try to sort a dictionary; it might be doable, however, it probably won't give the best performances. Instead of a javascript object, you should probably opt for an array. Something like:

var todos = [{id:1}, {id:2]}, {id:3}];
todos.sort(function (a, b) { return b.id - a.id; });
Jazzwave06
  • 1,883
  • 1
  • 11
  • 19
  • I removed these references, however, json and javascript object are the same thing, from the javascript runtime point of view. – Jazzwave06 Jul 28 '14 at 18:04
  • Of course not. `var thisIsJSON = '{"hello": "world"}';` is entirely different from `var thisIsObject = {hello: "world"};`. – Álvaro González Jul 28 '14 at 18:10
  • Because you are treating the json as a litteral. JSON stands for javascript object notation. This clearly is the notation of an object in javascript. – Jazzwave06 Jul 28 '14 at 18:13
  • The term JSON in web technologies has a very specific meaning: it's a *lightweight data-interchange format* as defined at [json.org](http://json.org/). And it's syntax is a subset of JavaScript. Now, JavaScript itself is a programming language and it's syntax is subset of English. But when you read Shakespeare you are not reading JavaScript. – Álvaro González Jul 28 '14 at 18:18
  • I know what json is, thank you. You simply nerd out on some definition. `var thisIsObject2 = eval(thisIsJSON);` There, you got your object from the json. The only difference is the object was interpreted; the json as yet to be interpreted. In the end, a javascript object using the {} syntax IS json, because it IS a javascript object notation. – Jazzwave06 Jul 28 '14 at 18:21
  • Please accept my excuses if I've annoyed you in some way. I was not trying to be pedantic. It's only that things have names for a reason. I've seen many questions here where someone would feed an API call with arbitrary JavaScript code and then wonder why it was failing. I'm sure you do know the difference between computer languages and serialisation data formats and you're as interested as everybody else in not spreading more misinformation among those who can't tell the difference. – Álvaro González Jul 28 '14 at 18:36
  • This is a better answer. To me, it is the samething, but I indeed had not tought of it as confusing for others. I'll try to be the most specific possible in future response to avoid this confusion. – Jazzwave06 Jul 28 '14 at 18:39
0

The problem is that todos is object of objects while the function sort is for arrays, all you need to do is to convert your todos to an array and sort the array just like this

var todos_array = [];
for(key in model.todos) 
   todos_array.push(model.todos[key]);

then you can sort your array :

todos_array = todos_array.sort(sortItem);
Khalid
  • 4,730
  • 5
  • 27
  • 50
0

Arrays are easier to sort as others have mentioned. I would add that your first line is problematic: You are declaring a variable that contains a '.' and dots are not allowed in variable names.

You may have meant to do something like this:

var model = {}; // serves as a namespace for your models
model.todos = {
  1:{"id":1,"text":"ksjdv;lv","completed":false},
  2:{"id":2,"text":"lsakckcv","completed":false},
  3:{"id":3,"text":"sakvalvs","completed":false}
}
rob.luton
  • 395
  • 1
  • 4
  • 7