Sorting Data Attributes Alphabetically [duplicate]
This question already has an answer here:
Sort divs in jQuery based on attribute 'data-sort'?
4 answers
My code doesn't seem to access the data attribute name and I have tried many different codes.
<div class="columns offer_table_rows" style=" position:relative;" data-personal="1" data-euro="" data-vehicle="122" data-roadside="neutral false " data-national="neutral false " data-home="neutral true" data-onward="neutral true" data-name="Different Brand 1">
$(".brandName").on('click', function() {
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return $(a).dataset('name').toLowerCase() > $(b).dataset('name').toLowerCase();
}).appendTo($wrapper);
})
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return b.dataset.name - a.dataset.name;
})
When the brandName filter button is clicked the wrapper div finds a row and sorts using data attribute name. I've tried both of the above variations but none seem to access the data attribute, or I just don't know what is going wrong.
I know for sure the wrapper finds the row and it all works until the data attribute is searched as I have number sorts that do work with the same code.
EDIT: This is not a duplicate as the supposed answer doesnt answer this question.
using $(a).data instead of what i have doesnt work. I am able to console.log the data names if I use
console.log($(a).data('name')) but they dont seem to sort
EDIT2:
I can console.log true false when evaluating if data.a is bigger than data.b =
`$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { // console.log(a.dataset.name >b.dataset.name); }) })
But if I try to make it a function that returns and appends to the wrapper it does nothing
javascript jquery html sorting
marked as duplicate by LGSon
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 11:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Sort divs in jQuery based on attribute 'data-sort'?
4 answers
My code doesn't seem to access the data attribute name and I have tried many different codes.
<div class="columns offer_table_rows" style=" position:relative;" data-personal="1" data-euro="" data-vehicle="122" data-roadside="neutral false " data-national="neutral false " data-home="neutral true" data-onward="neutral true" data-name="Different Brand 1">
$(".brandName").on('click', function() {
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return $(a).dataset('name').toLowerCase() > $(b).dataset('name').toLowerCase();
}).appendTo($wrapper);
})
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return b.dataset.name - a.dataset.name;
})
When the brandName filter button is clicked the wrapper div finds a row and sorts using data attribute name. I've tried both of the above variations but none seem to access the data attribute, or I just don't know what is going wrong.
I know for sure the wrapper finds the row and it all works until the data attribute is searched as I have number sorts that do work with the same code.
EDIT: This is not a duplicate as the supposed answer doesnt answer this question.
using $(a).data instead of what i have doesnt work. I am able to console.log the data names if I use
console.log($(a).data('name')) but they dont seem to sort
EDIT2:
I can console.log true false when evaluating if data.a is bigger than data.b =
`$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { // console.log(a.dataset.name >b.dataset.name); }) })
But if I try to make it a function that returns and appends to the wrapper it does nothing
javascript jquery html sorting
marked as duplicate by LGSon
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 11:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Use$(a).attr('data-name')
– shajji
Jan 3 at 11:38
you can also use$(a).data('name')
– shajji
Jan 3 at 11:40
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52
add a comment |
This question already has an answer here:
Sort divs in jQuery based on attribute 'data-sort'?
4 answers
My code doesn't seem to access the data attribute name and I have tried many different codes.
<div class="columns offer_table_rows" style=" position:relative;" data-personal="1" data-euro="" data-vehicle="122" data-roadside="neutral false " data-national="neutral false " data-home="neutral true" data-onward="neutral true" data-name="Different Brand 1">
$(".brandName").on('click', function() {
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return $(a).dataset('name').toLowerCase() > $(b).dataset('name').toLowerCase();
}).appendTo($wrapper);
})
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return b.dataset.name - a.dataset.name;
})
When the brandName filter button is clicked the wrapper div finds a row and sorts using data attribute name. I've tried both of the above variations but none seem to access the data attribute, or I just don't know what is going wrong.
I know for sure the wrapper finds the row and it all works until the data attribute is searched as I have number sorts that do work with the same code.
EDIT: This is not a duplicate as the supposed answer doesnt answer this question.
using $(a).data instead of what i have doesnt work. I am able to console.log the data names if I use
console.log($(a).data('name')) but they dont seem to sort
EDIT2:
I can console.log true false when evaluating if data.a is bigger than data.b =
`$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { // console.log(a.dataset.name >b.dataset.name); }) })
But if I try to make it a function that returns and appends to the wrapper it does nothing
javascript jquery html sorting
This question already has an answer here:
Sort divs in jQuery based on attribute 'data-sort'?
4 answers
My code doesn't seem to access the data attribute name and I have tried many different codes.
<div class="columns offer_table_rows" style=" position:relative;" data-personal="1" data-euro="" data-vehicle="122" data-roadside="neutral false " data-national="neutral false " data-home="neutral true" data-onward="neutral true" data-name="Different Brand 1">
$(".brandName").on('click', function() {
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return $(a).dataset('name').toLowerCase() > $(b).dataset('name').toLowerCase();
}).appendTo($wrapper);
})
$wrapper.find('.offer_table_rows').sort(function(a, b) {
return b.dataset.name - a.dataset.name;
})
When the brandName filter button is clicked the wrapper div finds a row and sorts using data attribute name. I've tried both of the above variations but none seem to access the data attribute, or I just don't know what is going wrong.
I know for sure the wrapper finds the row and it all works until the data attribute is searched as I have number sorts that do work with the same code.
EDIT: This is not a duplicate as the supposed answer doesnt answer this question.
using $(a).data instead of what i have doesnt work. I am able to console.log the data names if I use
console.log($(a).data('name')) but they dont seem to sort
EDIT2:
I can console.log true false when evaluating if data.a is bigger than data.b =
`$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { // console.log(a.dataset.name >b.dataset.name); }) })
But if I try to make it a function that returns and appends to the wrapper it does nothing
This question already has an answer here:
Sort divs in jQuery based on attribute 'data-sort'?
4 answers
javascript jquery html sorting
javascript jquery html sorting
edited Jan 4 at 12:43
Chris Cullen
asked Jan 3 at 11:36
Chris CullenChris Cullen
138
138
marked as duplicate by LGSon
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 11:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by LGSon
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 11:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Use$(a).attr('data-name')
– shajji
Jan 3 at 11:38
you can also use$(a).data('name')
– shajji
Jan 3 at 11:40
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52
add a comment |
Use$(a).attr('data-name')
– shajji
Jan 3 at 11:38
you can also use$(a).data('name')
– shajji
Jan 3 at 11:40
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52
Use
$(a).attr('data-name')
– shajji
Jan 3 at 11:38
Use
$(a).attr('data-name')
– shajji
Jan 3 at 11:38
you can also use
$(a).data('name')
– shajji
Jan 3 at 11:40
you can also use
$(a).data('name')
– shajji
Jan 3 at 11:40
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52
add a comment |
1 Answer
1
active
oldest
votes
Use localeCompare
.sort((a, b) => a.localeCompare(b));
1
a
andb
are Element objects, not strings.
– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer
– Chris Cullen
Jan 4 at 17:19
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use localeCompare
.sort((a, b) => a.localeCompare(b));
1
a
andb
are Element objects, not strings.
– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer
– Chris Cullen
Jan 4 at 17:19
add a comment |
Use localeCompare
.sort((a, b) => a.localeCompare(b));
1
a
andb
are Element objects, not strings.
– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer
– Chris Cullen
Jan 4 at 17:19
add a comment |
Use localeCompare
.sort((a, b) => a.localeCompare(b));
Use localeCompare
.sort((a, b) => a.localeCompare(b));
answered Jan 3 at 11:52
dddenisdddenis
150713
150713
1
a
andb
are Element objects, not strings.
– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer
– Chris Cullen
Jan 4 at 17:19
add a comment |
1
a
andb
are Element objects, not strings.
– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer
– Chris Cullen
Jan 4 at 17:19
1
1
a
and b
are Element objects, not strings.– Rory McCrossan
Jan 3 at 11:53
a
and b
are Element objects, not strings.– Rory McCrossan
Jan 3 at 11:53
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer– Chris Cullen
Jan 4 at 17:19
$(".brandName").on('click', function() { $wrapper.find('.offer_table_rows').sort(function(a, b) { var x = a.dataset.name.toLowerCase(); var y = b.dataset.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }).appendTo($wrapper); })
This is a Working answer– Chris Cullen
Jan 4 at 17:19
add a comment |
Use
$(a).attr('data-name')
– shajji
Jan 3 at 11:38
you can also use
$(a).data('name')
– shajji
Jan 3 at 11:40
For sorting based on data value - stackoverflow.com/questions/6133723/…
– Nitha
Jan 3 at 11:52