I want to sort on the source with jquery.

Asked 2 years ago, Updated 2 years ago, 100 views

What should I do if I want to rearrange the position on the source based on the coordinates of the position in jquery?

Example: Sort left values in descending order

<div style="position:relative;left:10px">1</div>
<div style="position:relative;left:0px">2</div>
<div style="position:relative;left:15px">3</div>
<div style="position:relative;left:5px">4</div>

<div style="position:relative;left:0px">2</div>
<div style="position:relative;left:5px">4</div>
<div style="position:relative;left:10px">1</div>
<div style="position:relative;left:15px">3</div>

javascript jquery jquery-ui

2022-09-30 11:13

1 Answers

var target=$('div');// Specify as all divs are covered


var sorted_obj = target.sort(function(a,b){
  return parseInt($(a).css('left'))-parseInt($(b).css('left'));
}));

target.parent().append(sorted_obj);


2022-09-30 11:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.