There is two arrays that display information appending elements to the document. They are being printed in ascending order, but when it comes to organize them all, I can’t organize them individually by data attribute.
This function does the sort but can’t mix different element classes:
JavaScript
x
10
10
1
$.fn.sortRaffles = function () {
2
this
3
.children()
4
.sort((a, b) => $(a).data("order") - $(b).data("order") || -1)
5
.appendTo(this);
6
7
return this;
8
}
9
$(".lista ul").sortRaffles();
10
Before the above code this is where I print the two different element classes (avaibleRaffle and reservedRaffle) using arrays of string and filtering them to display the correct result.
JavaScript
1
41
41
1
const nums = Array.from({ length: 11 }, (_, i) => `${i}`.padStart(3, "0"));
2
3
const customers = [
4
{ number: "002" },
5
{ number: "6003" },
6
{ number: "1010" },
7
{ number: "008,005,007,0002" }
8
];
9
10
const reserved_nums = nums.filter(
11
(s) => !customers.some((o) => o.number.includes(s))
12
);
13
14
const avaible_nums = nums.filter((s) =>
15
customers.some((o) => o.number.includes(s))
16
);
17
18
var avaibleRaffle = $();
19
$.each(avaible_nums, function (index, value) {
20
avaibleRaffle = avaibleRaffle.add(
21
'<span class="btn btn-success btn_reservas data-order="' +
22
value +
23
'" >' +
24
value +
25
"</span>"
26
);
27
});
28
$(".lista ul li").append(avaibleRaffle);
29
30
var reservedRaffle = $();
31
$.each(reserved_nums, function (index, value) {
32
reservedRaffle = reservedRaffle.add(
33
'<span class="btn btn-warning btn_pagos data-order="' +
34
value +
35
'" >' +
36
value +
37
"</span>"
38
);
39
});
40
$(".lista ul li").append(reservedRaffle);
41
The a minimal reproducible example:
JavaScript
1
55
55
1
/* eslint-env jquery */
2
3
const nums = Array.from({ length: 11 }, (_, i) => `${i}`.padStart(3, "0"));
4
5
const customers = [
6
{ number: "002" },
7
{ number: "6003" },
8
{ number: "1010" },
9
{ number: "008,005,007,0002" }
10
];
11
12
const reserved_nums = nums.filter(
13
(s) => !customers.some((o) => o.number.includes(s))
14
);
15
16
const avaible_nums = nums.filter((s) =>
17
customers.some((o) => o.number.includes(s))
18
);
19
20
var avaibleRaffle = $();
21
$.each(avaible_nums, function (index, value) {
22
avaibleRaffle = avaibleRaffle.add(
23
'<span class="btn btn-success btn_reservas data-order="' +
24
value +
25
'" >' +
26
value +
27
"</span>"
28
);
29
});
30
$(".lista ul li").append(avaibleRaffle);
31
32
var reservedRaffle = $();
33
$.each(reserved_nums, function (index, value) {
34
reservedRaffle = reservedRaffle.add(
35
'<span class="btn btn-warning btn_pagos data-order="' +
36
value +
37
'" >' +
38
value +
39
"</span>"
40
);
41
});
42
$(".lista ul li").append(reservedRaffle);
43
44
$.fn.sortRaffles = function () {
45
this.children()
46
.sort((a, b) => $(a).data("order") - $(b).data("order") || -1)
47
.appendTo(this);
48
49
return this;
50
};
51
$(".lista ul li span").sortRaffles();
52
53
console.log("Desired Order:", JSON.stringify(nums));
54
console.log("Avaible:", JSON.stringify(avaible_nums));
55
console.log("Reserved:", JSON.stringify(reserved_nums));
JavaScript
1
12
12
1
.lista ul li {
2
display: inline;
3
}
4
5
.lista ul li span {
6
border-radius: 50%;
7
width: 100%;
8
max-width: 40px;
9
height: auto;
10
margin: 3px;
11
padding: 6px;
12
}
JavaScript
1
32
32
1
<html>
2
<head>
3
<title>Parcel Sandbox</title>
4
<meta charset="UTF-8" />
5
<link
6
type="text/css"
7
rel="stylesheet"
8
href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.1/semantic.css"
9
/>
10
<link
11
rel="stylesheet"
12
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
13
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l"
14
crossorigin="anonymous"
15
/>
16
17
<link type="text/css" rel="stylesheet" href="src/styles.css" />
18
</head>
19
20
<body>
21
<div class="lista">
22
<ul>
23
<li>
24
<span></span>
25
</li>
26
</ul>
27
</div>
28
29
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
30
<script src="src/index.js"></script>
31
</body>
32
</html>
Advertisement
Answer
Since I couldn’t resolve this problem of printing in ascending order the numbers after validating it with sort. Besides the collaboration, I came with a solution that is only using forEach for comparison of arrays:
JavaScript
1
11
11
1
nums.forEach(function (value) {
2
if (avaibleNumbers.indexOf(value) != -1) {
3
var av = avaibleNumbers.indexOf(value);
4
console.log(avaibleNumbers[av]);
5
}
6
else if (reservedNumbers.indexOf(value) != -1) {
7
var rs = reservedNumbers.indexOf(value);
8
console.log(reservedNumbers[rs]);
9
}
10
});
11