:ﺪﻨﮐ ﯽﻣ ﺐﺗﺮﻣ ﺎﺒﻔﻟﺍ ﻑﻭﺮﺣ ﺱﺎﺳﺍ ﺮﺑ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ sort()
ﺵﻭﺭ
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>The sort() method sorts an array alphabetically:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.sort();
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
.ﺪﻨﮐ ﯽﻣ ﺱﻮﮑﻌﻣ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﺮﺻﺎﻨﻋ reverse()
ﺵﻭﺭ
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻥﺁ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>Sort in Reverse</h2>
<p>The reverse() method reverses the elements in an array.</p>
<p>By combining sort() and reverse() you can sort an array in descending order:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
// Create and display an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
// First sort the array
fruits.sort();
// Then reverse it:
fruits.reverse();
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
.ﺪﻨﮐ ﯽﻣ ﺐﺗﺮﻣ ﻪﺘﺷﺭ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﺮﯾﺩﺎﻘﻣ sort()
ﻊﺑﺎﺗ ،ﺽﺮﻓ ﺶﯿﭘ ﺭﻮﻃ ﻪﺑ
.(ﺖﺳﺍ ﻩﺪﻣﺁ "Banana" ﺯﺍ ﻞﺒﻗ "Apple") ﺪﻨﮐ ﯽﻣ ﺭﺎﮐ ﺎﻫ ﻪﺘﺷﺭ ﯼﺍﺮﺑ ﯽﺑﻮﺧ ﻪﺑ ﻦﯾﺍ
،ﺖﺳﺍ "100" ﺯﺍ ﺮﺘﮔﺭﺰﺑ "25" ،ﺪﻧﻮﺷ ﺐﺗﺮﻣ ﻪﺘﺷﺭ ﻥﺍﻮﻨﻋ ﻪﺑ ﺩﺍﺪﻋﺍ ﺮﮔﺍ ،ﻝﺎﺣ ﻦﯾﺍ ﺎﺑ
.ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﯽﺘﺳﺭﺩﺎﻧ ﻪﺠﯿﺘﻧ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﻡﺎﮕﻨﻫ sort()
ﺵﻭﺭ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ
:ﺪﯿﻨﮐ ﻑﺮﻃﺮﺑ ﺍﺭ ﻞﮑﺸﻣ ﻦﯾﺍ ﻪﺴﯾﺎﻘﻣ ﺩﺮﮑﻠﻤﻋ ﮏﯾ ﻪﺋﺍﺭﺍ ﺎﺑ ﺪﯿﻧﺍﻮﺗﯽﻣ
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Sort the array in ascending order:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo1").innerHTML = points;
points.sort(function(a, b){return a - b});
document.getElementById("demo2").innerHTML = points;
</script>
</body>
</html>
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﯽﻟﻭﺰﻧ ﺕﺭﻮﺻ ﻪﺑ ﻪﯾﺍﺭﺁ ﮏﯾ ﻥﺩﺮﮐ ﺐﺗﺮﻣ ﯼﺍﺮﺑ ﺪﻨﻓﺮﺗ ﻦﯿﻤﻫ ﺯﺍ
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Sort the array in descending order:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo1").innerHTML = points;
points.sort(function(a, b){return b - a});
document.getElementById("demo2").innerHTML = points;
</script>
</body>
</html>
ﺖﺳﺍ ﻦﯾﺰﮕﯾﺎﺟ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﮏﯾ ﻒﯾﺮﻌﺗ ﻪﺴﯾﺎﻘﻣ ﻊﺑﺎﺗ ﻑﺪﻫ
ﺪﻧﺍﺩﺮﮔﺮﺑ ﺍﺭ ﺖﺒﺜﻣ ﺎﯾ ﺮﻔﺻ ،ﯽﻔﻨﻣ ﺭﺍﺪﻘﻣ ﺪﯾﺎﺑ ،ﻦﯾﺍ ﻪﺑ ﻪﺘﺴﺑ ﻪﺴﯾﺎﻘﻣ ﻊﺑﺎﺗ
function(a, b){return a - b}
ﻪﺑ ﺍﺭ ﺮﯾﺩﺎﻘﻣ ،ﺪﻨﮐ ﯽﻣ ﻪﺴﯾﺎﻘﻣ ﺍﺭ ﺭﺍﺪﻘﻣ ﻭﺩ sort()
ﻊﺑﺎﺗ ﯽﺘﻗﻭ
ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﺯﺍ ﻞﺒﻗ a
،ﺪﺷﺎﺑ ﯽﻔﻨﻣ ﻪﺠﯿﺘﻧ ﺮﮔﺍ
ﺩﻮﺷ ﯽﻣ ﺐﺗﺮﻣ b
،ﺪﺷﺎﺑ ﺖﺒﺜﻣ ﻪﺠﯿﺘﻧ ﺮﮔﺍ
ﺩﻮﺷ ﯽﻤﻧ ﻡﺎﺠﻧﺍ ﻭﺩ ﻦﯾﺍ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﺐﯿﺗﺮﺗ ﺎﺑ ﯼﺮﯿﯿﻐﺗ ﭻﯿﻫ ،ﺪﺷﺎﺑ 0 ﻪﺠﯿﺘﻧ ﺮﮔﺍ
ﻝﺎﺜﻣ:
ﺪﻨﮐ ﯽﻣ ﻪﺴﯾﺎﻘﻣ a ﺭﺩ ﺭﺍﺪﻘﻣ ﻭﺩ ﺎﺑ ﺍﺭ ﻪﯾﺍﺭﺁ ﺭﺩ ﺩﻮﺟﻮﻣ ﺮﯾﺩﺎﻘﻣ ﻡﺎﻤﺗ compare ﻊﺑﺎﺗ
.ﺪﻨﮐ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺍﺭ (100 ،40) ﻪﺴﯾﺎﻘﻣ ﻊﺑﺎﺗ sort()
ﺵﻭﺭ ،100 ﻭ 40 ﻪﺴﯾﺎﻘﻣ ﻡﺎﮕﻨﻫ
ﻭ ،ﺪﻨﮐ ﯽﻣ ﻪﺒﺳﺎﺤﻣ ﺍﺭ (a - b)
100 - 40 ﻊﺑﺎﺗ
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻭ ﯼﺩﺪﻋ ﺶﯾﺎﻣﺯﺁ ﯼﺍﺮﺑ ﺪﮐ ﻪﻌﻄﻗ ﻦﯾﺍ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
<button onclick="myFunction1()">Sort Alphabetically</button>
<button
onclick="myFunction2()">Sort Numerically</button>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function
myFunction1() {
points.sort();
document.getElementById("demo").innerHTML
= points;
}
function myFunction2() {
points.sort(function(a, b){return
a - b});
document.getElementById("demo").innerHTML = points;
}
</script>
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Click the buttons to sort the array alphabetically or numerically.</p>
<button onclick="myFunction1()">Sort Alphabetically</button>
<button onclick="myFunction2()">Sort Numerically</button>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction1() {
points.sort();
document.getElementById("demo").innerHTML = points;
}
function myFunction2() {
points.sort(function(a, b){return a - b});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(){return 0.5 - Math.random()});
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Click the button (again and again) to sort the array in random order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
points.sort(function(){return 0.5 - Math.random()});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
ﺩﻮﺑ ﺪﻫﺍﻮﺧ ﯽﺧﺮﺑ ﻊﻔﻧ ﻪﺑ .ﺖﺴﯿﻧ ﻖﯿﻗﺩ ،()em>array.sort> ،ﻻﺎﺑ ﻝﺎﺜﻣ
ﺩﻮﺑ ﻭ ﺩﻮﺷ ﯽﻣ ﻩﺪﯿﻣﺎﻧ ﻞﻓﺎﺷ ﺲﺘﯾ ﺮﺸﯿﻓ ،ﺢﯿﺤﺻ ﺵﻭﺭ ﻦﯾﺮﺗ ﺏﻮﺒﺤﻣ
:ﺩﺮﮐ ﻪﻤﺟﺮﺗ ﻞﮑﺷ ﻦﯾﺍ ﻪﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺵﻭﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ
const points = [40, 100, 1, 5, 25, 10];
for (let i = points.length -1; i > 0; i--) {
let j = Math.floor(Math.random() * (i+1));
let k = points[i];
points[i] = points[j];
points[j] = k;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Array Sort</h1>
<h2>The Fisher Yates Method</h2>
<p>Click the button (again and again) to sort the array in random order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
for (let i = points.length -1; i > 0; i--) {
let j = Math.floor(Math.random() * (i+1));
let k = points[i];
points[i] = points[j];
points[j] = k;
}
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
ﺩﺭﺍﺪﻧ ﺩﻮﺟﻭ ﻞﻗﺍﺪﺣ ﺎﯾ ﺮﺜﮐﺍﺪﺣ ﻦﺘﻓﺎﯾ ﯼﺍﺮﺑ ﯽﻠﺧﺍﺩ ﯽﻌﺑﺍﻮﺗ ﭻﯿﻫ
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻥﺁ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ،ﻪﯾﺍﺭﺁ ﮏﯾ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﺯﺍ ﺲﭘ ،ﻝﺎﺣ ﻦﯾﺍ ﺎﺑ
:ﯼﺩﻮﻌﺻ ﯼﺯﺎﺳ ﺐﺗﺮﻣ
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
// now points[0] contains the lowest value
// and points[points.length-1] contains the highest value
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>The lowest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points[0];
</script>
</body>
</html>
:ﯽﻟﻭﺰﻧ ﯼﺯﺎﺳ ﺐﺗﺮﻣ
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
// now points[0] contains the highest value
// and points[points.length-1] contains the lowest value
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>The highest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});
document.getElementById("demo").innerHTML = points[0];
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﺍﺭ ﺭﺍﺪﻘﻣ (ﻦﯾﺮﺘﻤﮐ ﺎﯾ) ﻦﯾﺮﺗﻻﺎﺑ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻂﻘﻓ ﺮﮔﺍ ﺖﺳﺍ ﺪﻣﺁﺭﺎﮐﺎﻧ ﺭﺎﯿﺴﺑ ﯽﺷﻭﺭ ﻪﯾﺍﺭﺁ ﻞﮐ ﯼﺯﺎﺳ ﺐﺗﺮﻣ
Math.max()
ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ :ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﯾﺍﺭﺁ ﮏﯾ ﺭﺩ ﺩﺪﻋ ﻦﯾﺮﺗﻻﺎﺑ ﻦﺘﻓﺎﯾ ﯼﺍﺮﺑ Math.max.apply
ﺯﺍ ﺪﯿﻧﺍﻮﺗﯽﻣ
function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Array Sort</h1>
<p>The highest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMax(points);
function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
</script>
</body>
</html>
Math.max.apply(null, [1, 2, 3])
ﻝﺩﺎﻌﻣ Math.max(1, 2) 3 ، ﺖ
Math.min()
ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ :ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﯾﺍﺭﺁ ﮏﯾ ﺭﺩ ﺩﺪﻋ ﻦﯾﺮﺘﻤﮐ ﻦﺘﻓﺎﯾ ﯼﺍﺮﺑ Math.min.apply
ﺯﺍ ﺪﯿﻧﺍﻮﺗﯽﻣ
function myArrayMin(arr) {
return Math.min.apply(null, arr);
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Array Sort</h1>
<p>The lowest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMin(points);
function myArrayMin(arr) {
return Math.min.apply(null, arr);
}
</script>
</body>
</html>
Math.min.apply(null, [1, 2, 3])
ﻝﺩﺎﻌﻣ Math.min(1, 2) 3 ، ﺖ
.ﺖﺳﺍ "ﻪﺘﺧﺎﺳ ﻪﻧﺎﺧ" ﺵﻭﺭ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻞﺣ ﻩﺍﺭ ﻦﯾﺮﺘﻌﯾﺮﺳ
ﺪﻨﮐ ﯽﻣ ﻪﺴﯾﺎﻘﻣ ﻦﯾﺮﺗﻻﺎﺑ ﺎﺑ ﺍﺭ ﺭﺍﺪﻘﻣ ﺮﻫ ﻭ ﺪﻧﺯ ﯽﻣ ﻪﻘﻠﺣ ﻪﯾﺍﺭﺁ ﮏﯾ ﺭﺩ ﻊﺑﺎﺗ ﻦﯾﺍ
function myArrayMax(arr) {
let len = arr.length;
let max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>The highest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMax(points);
function myArrayMax(arr) {
let len = arr.length;
let max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}
</script>
</body>
</html>
ﺪﻨﮐ ﯽﻣ ﻪﺴﯾﺎﻘﻣ ﻦﯾﺮﺘﻤﮐ ﺎﺑ ﺍﺭ ﺭﺍﺪﻘﻣ ﺮﻫ ﻭ ﺪﻧﺯ ﯽﻣ ﻪﻘﻠﺣ ﻪﯾﺍﺭﺁ ﮏﯾ ﺭﺩ ﻊﺑﺎﺗ ﻦﯾﺍ
function myArrayMin(arr) {
let len = arr.length;
let min = Infinity;
while (len--) {
if (arr[len] < min) {
min = arr[len];
}
}
return min;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>The lowest number is <span id="demo"></span>.</p>
<script>
const points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = myArrayMin(points);
function myArrayMin(arr) {
let len = arr.length;
let min = Infinity;
while (len--) {
if (arr[len] < min) {
min = arr[len];
}
}
return min;
}
</script>
</body>
</html>
:ﺪﻨﺘﺴﻫ ءﺎﯿﺷﺍ ﯼﻭﺎﺣ ﺐﻠﻏﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫ ﻪﯾﺍﺭﺁ
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
sort()
ﺵﻭﺭ ،ﺪﻨﺷﺎﺑ ﻒﻠﺘﺨﻣ ﯼﺎﻫ ﻩﺩﺍﺩ ﻉﺍﻮﻧﺍ ﺕﺎﯿﺻﻮﺼﺧ ﯼﺍﺭﺍﺩ ﺎﯿﺷﺍ ﺮﮔﺍ ﯽﺘﺣ
:ﺪﯿﺴﯾﻮﻨﺑ ﯽﮔﮋﯾﻭ ﺮﯾﺩﺎﻘﻣ ﻪﺴﯾﺎﻘﻣ ﯼﺍﺮﺑ ﻪﺴﯾﺎﻘﻣ ﻊﺑﺎﺗ ﮏﯾ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ ﻞﺣ ﻩﺍﺭ
cars.sort(function(a, b){return a.year - b.year});
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Sort car objects on age:</p>
<p id="demo"></p>
<script>
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
displayCars();
cars.sort(function(a, b){return a.year - b.year});
displayCars();
function displayCars() {
document.getElementById("demo").innerHTML =
cars[0].type + " " + cars[0].year + "<br>" +
cars[1].type + " " + cars[1].year + "<br>" +
cars[2].type + " " + cars[2].year;
}
</script>
</body>
</html>
:ﺖﺳﺍ ﺮﺗ ﻩﺪﯿﭽﯿﭘ ﯽﻤﮐ ﻪﺘﺷﺭ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﻪﺴﯾﺎﻘﻣ
cars.sort(function(a, b){
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The sort() Method</h2>
<p>Click the buttons to sort car objects on type.</p>
<button onclick="myFunction()">Sort</button>
<p id="demo"></p>
<script>
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
displayCars();
function myFunction() {
cars.sort(function(a, b){
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
displayCars();
}
function displayCars() {
document.getElementById("demo").innerHTML =
cars[0].type + " " + cars[0].year + "<br>" +
cars[1].type + " " + cars[1].year + "<br>" +
cars[2].type + " " + cars[2].year;
}
</script>
</body>
</html>
sort()
ﺭﺍﺪﯾﺎﭘ ﻪﯾﺍﺭﺁES2019 ﻪﯾﺍﺭﺁ ﺵﻭﺭ sort()
ﺍﺭ ﺩﺮﮐ ﯽﻨﯿﺑﺯﺎﺑ.
.ﺩﺍﺩ ﯽﻣ ﻩﺯﺎﺟﺍ QuickSort ﺪﻨﻧﺎﻣ ﺭﺍﺪﯾﺎﭘﺎﻧ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﯼﺎﻫ ﻢﺘﯾﺭﻮﮕﻟﺍ ﻪﺑ ﺕﺎﺼﺨﺸﻣ ﻦﯾﺍ ،2019 ﻝﺎﺳ ﺯﺍ ﻞﺒﻗ
:ﺪﻨﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺭﺍﺪﯾﺎﭘ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﻢﺘﯾﺭﻮﮕﻟﺍ ﮏﯾ ﺯﺍ ﺪﯾﺎﺑ ﺎﻫﺮﮔﺭﻭﺮﻣ ،ES2019 ﺯﺍ ﺪﻌﺑ
.ﺪﻨﻨﮐ ﻆﻔﺣ ﺭﺍﺪﻘﻣ ﻥﺎﻤﻫ ﺎﺑ ﺮﺻﺎﻨﻋ ﺮﯾﺎﺳ ﻪﺑ ﺖﺒﺴﻧ ﺍﺭ ﺩﻮﺧ ﯽﺒﺴﻧ ﺖﯿﻌﻗﻮﻣ ﺪﯾﺎﺑ ﺮﺻﺎﻨﻋ ،ﺭﺍﺪﻘﻣ ﮏﯾ ﯼﻭﺭ ﺮﺑ ﺮﺻﺎﻨﻋ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﻡﺎﮕﻨﻫ
const myArr = [
{name:"X00",price:100 },
{name:"X01",price:100 },
{name:"X02",price:100 },
{name:"X03",price:100 },
{name:"X04",price:110 },
{name:"X05",price:110 },
{name:"X06",price:110 },
{name:"X07",price:110 }
];
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Stable Sort</h1>
<p>From ES2019, browsers must use a stable sorting algorithm.</p>
<p>When sorting elements on a key, the elements must keep their relative position to other objects with the same key.</p>
<p id="demo"></p>
<script>
const myArr = [
{name:"X00",price:100 },
{name:"X01",price:100 },
{name:"X02",price:100 },
{name:"X03",price:100 },
{name:"X04",price:110 },
{name:"X05",price:110 },
{name:"X06",price:110 },
{name:"X07",price:110 },
{name:"X08",price:120 },
{name:"X09",price:120 },
{name:"X10",price:120 },
{name:"X11",price:120 },
{name:"X12",price:130 },
{name:"X13",price:130 },
{name:"X14",price:130 },
{name:"X15",price:130 },
{name:"X16",price:140 },
{name:"X17",price:140 },
{name:"X18",price:140 },
{name:"X19",price:140 }
];
myArr.sort( (p1, p2) => {
if (p1.price < p2.price) return -1;
if (p1.price > p2.price) return 1;
return 0;
});
let txt = "";
myArr.forEach(myFunction);
function myFunction(value) {
txt += value.name + " " + value.price + "<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
ﺩﻮﺷ ﺮﻫﺎﻇ ﺎﻫ ﻡﺎﻧ ﺎﺑ ﺖﺴﯿﻧ ﺯﺎﺠﻣ ﻪﺠﯿﺘﻧ ،ﺖﻤﯿﻗ ﺱﺎﺳﺍ ﺮﺑ ﯼﺯﺎﺳ ﺐﺗﺮﻣ ﻡﺎﮕﻨﻫ ،ﻻﺎﺑ ﻝﺎﺜﻣ ﺭﺩ
X01 100
X03 100
X00 100
X03 100
X05 110
X04 110
X06 110
X07 110
:ﺪﯾﻭﺮﺑ ﺎﻣ ﺱﺭﺩﺁ ﻪﺑ ،ﻪﯾﺍﺭﺁ ﻞﻣﺎﮐ ﻊﺟﺮﻣ ﮏﯾ ﯼﺍﺮﺑ
.ﻞﻣﺎﮐ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﯾﺍﺭﺁ ﻊﺟﺮﻣ
ﺖﺳﺍ ﺎﻫ ﻪﯾﺍﺭﺁ ﻪﻤﻫ ﺯﺍ ﯽﯾﺎﻫ ﻪﻧﻮﻤﻧ ﻭ ﺕﺎﺤﯿﺿﻮﺗ ﯼﻭﺎﺣ ﻊﺟﺮﻣ