3

I used this question (jQuery sort elements using data id) to get a lot of work done on a project I'm doing.

The top voted answer mentions that using jQuery;s .data() is required if I need it to work in IE10 and below. I haven't tested it in any of those browsers, but I have found that it does not work in IE11 or Edge.

Here's the jsfiddle that works just fine in Chrome for me: http://jsfiddle.net/4o771n7o/

HTML

<div class="clist">
    <div data-sid=1>1</div>
    <div data-sid=4>4</div>
    <div data-sid=3>3</div>
    <div data-sid=1>1</div>
    <div data-sid=4>4</div>
    <div data-sid=2>2</div>
    <div data-sid=1>1</div>
</div>

Javascript

$('.clist div').sort(function(a,b) {
     return $(a).data('sid') > $(b).data('sid');
}).appendTo('.clist');
3
  • And that fails to work in IE11 and Edge? Commented Dec 7, 2015 at 21:03
  • Can you explain how it fails? As in does it not sort at all, is there an error message? Commented Dec 7, 2015 at 21:03
  • It doesn't sort at all for me. No error message in the console or anything. Commented Dec 7, 2015 at 21:15

3 Answers 3

4

The sort function is bad; I'm not sure why it works for any other browser. Working JSFiddle:

http://jsfiddle.net/0m75k1fm/

The sort function should return a number, not a boolean:

$('.clist div').sort(function(a,b) {
     return parseInt($(a).data('sid'), 10) - parseInt($(b).data('sid'), 10);
}).appendTo('.clist');
Sign up to request clarification or add additional context in comments.

12 Comments

Looks like IE can handle the missing quotes; it's just the sort function that is specifically tripping it up.
Basically you didn't change anything. Quotes are optional in this case and > handles to number conversion behind the scene.
The attributes are not malformed. Yes, changing it to subtract does help because the compareFunction expects an integer but it doesn't really answer the question.
@dfsq: look carefully.
@dfsq: Sort functions should return -1, 0, or 1 to indicate ordering. Just because JS can convert Boolean to Number doesn't mean the sort function is correct.
|
2

You can use

$('.clist div').sort(function(a,b) {
     return $(a).attr('data-sid') > $(b).attr('data-sid') ? 1 : -1;
}).appendTo('.clist');

Works in edge

1 Comment

Did you test this in IE11 and Edge?
0

You can sort HTML elements pretty easily without jQuery. Just use the built in document.querySelectorAll function to select your collection of HTML elements (as a nodelist) and then use the built in Array.prototype.sort.apply(YourCollection,[SortFunction]) to invoke the Array object's sort function on your nodeList.

var nodes = document.querySelectorAll(".clist [data-sid]");

Array.prototype.sort.apply(nodes,
[function(first, second) {
    var a = +first.getAttribute("data-sid"),
      b = +second.getAttribute("data-sid");
    return a > b ? 1 : (a < b ? -1 : 0);
  }]);

for (i = 0, len = nodes.length; i < len; i++) {
  document.querySelector(".clist").appendChild(nodes[i]);
}
[data-sid] {
  border: 1px solid black;
}
[data-sid="1"] {
  background-color: rgb(255, 250, 250);
}
[data-sid="2"] {
  background-color: rgb(255, 200, 200);
}
[data-sid="3"] {
  background-color: rgb(255, 100, 100);
}
[data-sid="4"] {
  background-color: rgb(255, 50, 50);
}
<div class="clist">
  <div data-sid=1>1</div>
  <div data-sid=4>4</div>
  <div data-sid=3>3</div>
  <div data-sid=1>1</div>
  <div data-sid=4>4</div>
  <div data-sid=2>2</div>
  <div data-sid=1>1</div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.