Chapterبەش 7 · Lectureوانە 7.4

Sorting Arraysڕیزکردنی ڕیزەکەکان

Put arrays in order with PHP's six sort functions. Learn what sort, rsort, asort, arsort, ksort and krsort do, a simple way to remember their names, which ones keep the keys, why sorting changes the original array, and how mixed values and capital letters are ordered.

بە شەش PHP ڕێک بخە. فێر ببە sort، rsort، asort، arsort، ksort و krsort چی دەکەن، ڕێگایەکی ئاسان بۆ لەبەرکردنی ناوەکانیان، کامیان دەپارێزن، بۆچی ڕەسەنەکە دەگۆڕێت و تێکەڵ و پیتی گەورە چۆن ڕیز دەکرێن.

16 min read15 خولەک خوێندنەوە · 16 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain ascending and descending order.
  • Use sort() and rsort() on indexed arrays.
  • Use asort(), arsort(), ksort() and krsort() on associative arrays, keeping the keys.
  • Remember the function names with the letters k, a and r.
  • Know that sort functions change the array in place and return true.
  • Predict the order of mixed values, capital letters and numeric strings.
  • و ڕوون بکەیتەوە.
  • sort() و rsort() لەسەر بەکاربهێنیت.
  • asort()، arsort()، ksort() و krsort() لەسەر بەکاربهێنیت و بپارێزیت.
  • ناوی بە یارمەتیی پیتەکانی k، a و r لەبەر بکەیت.
  • بزانیت لە شوێنی خۆیدا دەگۆڕن و true دەگەڕێننەوە.
  • ڕیزبەندیی تێکەڵ، پیتی گەورە و ژمارەیی پێشبینی بکەیت.

Why sort an array?بۆچی ڕیزەکێک ڕیز دەکەین؟

Lists are easier to read when they are in order: names from A to Z, prices from cheapest to most expensive, marks from highest to lowest. Putting the elements of an array in order is called sorting. PHP has built-in functions that sort an array for you.

لیستەکان کاتێک ڕێکخراون ئاسانتر دەخوێنرێنەوە: ناوەکان لە A تا Z، نرخی کاڵاکان لە هەرزانترینەوە بۆ گرانترین، نمرەکان لە بەرزترینەوە بۆ نزمترین. ڕێکخستنی بە پێی ڕیزبەندییەک پێی دەوترێت . PHP چەند هەیە کە بۆ ڕیز دەکەن.

Definitionپێناسە

Putting items in a fixed order, for example numbers from the smallest to the largest (), the newest files first () or names in alphabetical order. A , a and many other programs can sort .

دانانی شتەکانە بە ڕیزبەندییەکی دیاریکراو، بۆ نموونە ژمارەکان لە بچووکترینەوە بۆ گەورەترین ()، نوێترین فایلەکان لە سەرەتادا () یان ناوەکان بەپێی ئەلفوبێ. ، و زۆر بەرنامەی تر دەتوانن ڕیز بکەن.

  • Ascending order: from small to big: 1, 2, 3 … or A to Z.
  • Descending order: from big to small: 3, 2, 1 … or Z to A.
  • : لە بچووکەوە بۆ گەورە: 1، 2، 3 … یان A تا Z.
  • : لە گەورەوە بۆ بچووک: 3، 2، 1 … یان Z تا A.
Lining up for the class photoڕیزبەستن بۆ وێنەی پۆل

Imagine students lining up for a class photo, shortest first. If every student wears a name badge, the badges move with the students: you can still tell who is who. That is asort(). If instead the photographer takes the badges away and gives the students new numbers 1, 2, 3 by position, you lose the names. That is sort().

وێنای بکە خوێندکاران بۆ وێنەی پۆل ڕیز دەبەستن، کورتترین یەکەم. ئەگەر هەر خوێندکارێک کارتی ناوی پێوە بێت، کارتەکان لەگەڵ خوێندکاران دەجوڵێن: هێشتا دەزانیت کێ کێیە. ئەمە asort()ـە. بەڵام ئەگەر وێنەگرەکە کارتەکان لێ بکاتەوە و بەپێی شوێن ژمارەی نوێی 1، 2، 3 بداتە خوێندکاران، ناوەکان ون دەبن. ئەمە sort()ـە.

Where the analogy stops workingسنووری ئەم نموونەیە

In the photo line the students physically move. A PHP sort function rearranges the same array in memory; it does not make a new copy.

لە ڕیزی وێنەکەدا خوێندکاران بە جەستە دەجوڵێن. PHP هەمان لە ڕێک دەخاتەوە؛ کۆپییەکی نوێ دروست ناکات.

The six sort functionsشەش کردارەکەی ڕیزکردن

The lab, the lecture summary and the course notes all give the same list of six functions:

تاقیگە، کورتەی وانەکان و تێبینییەکانی کۆرسەکە هەموویان هەمان لیستی شەش دەدەن:

Comparison of the six sort functionsبەراوردی شەش
FunctionDescription in the courseوەسف لە کۆرسەکەداSorts byبەپێی چی ڕیز دەکاتOrderڕیزبەندیKeys after sorting دوای
sort()sort arrays in ascending order بە ڕیز دەکاتvalueascendingبەرزبوونەوەnew keys 0, 1, 2 … نوێ 0، 1، 2 …
rsort()sort arrays in descending order بە ڕیز دەکاتvaluedescendingنزمبوونەوەnew keys 0, 1, 2 … نوێ 0، 1، 2 …
asort()sort associative arrays in ascending order, according to the value بە ڕیز دەکات، بەپێی valueascendingبەرزبوونەوەkeptدەمێننەوە
arsort()sort associative arrays in descending order, according to the value بە ڕیز دەکات، بەپێی valuedescendingنزمبوونەوەkeptدەمێننەوە
ksort()sort associative arrays in ascending order, according to the key بە ڕیز دەکات، بەپێی keyascendingبەرزبوونەوەkeptدەمێننەوە
krsort()sort associative arrays in descending order, according to the key بە ڕیز دەکات، بەپێی keydescendingنزمبوونەوەkeptدەمێننەوە

Three facts that the course list does not say, but that you must know:

سێ ڕاستی کە لیستەکەی کۆرسەکە نایڵێت، بەڵام دەبێت بیانزانیت:

  1. They sort the array in place. The function changes the array you give it. It does not return a new sorted array; it returns true. (See sorting changes the original.)
  2. sort() and rsort() throw the keys away and renumber the elements 0, 1, 2 … Use them for indexed arrays (lists).
  3. asort(), arsort(), ksort() and krsort() keep every key with its value. Use them for associative arrays.
  1. لە شوێنی خۆیدا ڕیز دەکەن. ئەو دەگۆڕێت کە پێی دەدەیت. نوێی ڕیزکراو ناگەڕێنێتەوە؛ true دەگەڕێنێتەوە. (بڕوانە ڕیزکردن ڕیزەکە ڕەسەنەکە دەگۆڕێت.)
  2. sort() و rsort() فڕێ دەدەن و لە نوێوە بە 0، 1، 2 … ژمارە دەکەن. بۆ (لیستەکان) بەکاریان بهێنە.
  3. asort()، arsort()، ksort() و krsort() هەر لەگەڵ دەهێڵنەوە. بۆ بەکاریان بهێنە.

The six sort functionsشەش کردارەکەی ڕیزکردن

All six on the same arrayهەر شەشیان لەسەر هەمان ڕیزەک

The start array has the keys Rezan, Aram and Shilan with the values 22, 19 and 21. Six panels in two rows. Top row (ascending): sort gives keys 0, 1, 2 with values 19, 21, 22 (new keys); asort gives Aram 19, Shilan 21, Rezan 22 (keys kept); ksort gives Aram 19, Rezan 22, Shilan 21 (by key). Bottom row (descending): rsort gives keys 0, 1, 2 with 22, 21, 19; arsort gives Rezan 22, Shilan 21, Aram 19; krsort gives Shilan 21, Rezan 22, Aram 19.ڕیزەکی سەرەتا کلیلەکانی Rezan، Aram و Shilanی هەیە بە نرخەکانی 22، 19 و 21. شەش بەش لە دوو ڕیزدا. ڕیزی سەرەوە (ڕیزی بەرزبوونەوە): sort کلیلەکانی 0، 1، 2 بە نرخەکانی 19، 21، 22 دەدات (کلیلی نوێ)؛ asort دەداتە Aram 19، Shilan 21، Rezan 22 (کلیلەکان دەمێننەوە)؛ ksort دەداتە Aram 19، Rezan 22، Shilan 21 (بەپێی کلیل). ڕیزی خوارەوە (ڕیزی نزمبوونەوە): rsort کلیلەکانی 0، 1، 2 بە 22، 21، 19 دەدات؛ arsort دەداتە Rezan 22، Shilan 21، Aram 19؛ krsort دەداتە Shilan 21، Rezan 22، Aram 19.
The same array after each of the six functions. Orange keys are new; green keys are kept.هەمان دوای هەریەک لە شەش . پرتەقاڵی نوێن؛ سەوز ماونەتەوە.CêIT
All six functions on the same arrayهەر شەش کردارەکە لەسەر هەمان ڕیزەکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function show($label, $array) {
    echo str_pad($label, 7) . ": ";
    foreach ($array as $key => $value) {
        echo "[$key] => $value  ";
    }
    echo "\n";
}

$start = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];

echo "<pre>";
$a = $start; sort($a);   show("sort", $a);
$a = $start; rsort($a);  show("rsort", $a);
$a = $start; asort($a);  show("asort", $a);
$a = $start; arsort($a); show("arsort", $a);
$a = $start; ksort($a);  show("ksort", $a);
$a = $start; krsort($a); show("krsort", $a);
echo "</pre>";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-8

A small helper function prints one line: the label, then every [key] => value.

یارمەتیدەری بچووک یەک دێڕ چاپ دەکات: ناونیشانەکە، پاشان هەموو [key] => valueـێک.

Lineدێڕی 13

$a = $start; makes a fresh copy, so every function starts from the same unsorted array.

$a = $start; کۆپییەکی نوێ دروست دەکات، بۆیە هەموو لە هەمان ڕیزنەکراوەوە دەست پێدەکات.

Lineدێڕی 13-18

Compare the keys: sort and rsort show 0, 1, 2; the other four keep the names.

بەراورد بکە: sort و rsort 0، 1، 2 پیشان دەدەن؛ چوارەکەی تر ناوەکان دەهێڵنەوە.

Quick checkپشکنینی خێراWhich two functions throw the keys away and give the elements new keys 0, 1, 2?کام دوو فڕێ دەدەن و نوێی 0، 1، 2 دەدەنە ؟

Remembering the namesلەبەرکردنی ناوەکان

The names look alike, but they are built from small letters added before sort. The lecture summary underlines the letters and gives the memory aid "sort KAR KR AR": sort, ksort, asort, rsort, krsort, arsort.

ناوەکان لە یەک دەچن، بەڵام لە چەند پیتێکی بچووک دروست بوون کە پێش sort زیاد دەکرێن. کورتەی وانەکان پیتەکان هێڵیان بەژێردا دەکێشێت و ڕێگایەکی لەبەرکردن دەدات، «⁦sort KAR KR AR⁩»: sort، ksort، asort، rsort، krsort، arsort.

  • k = key: sort by the keys.
  • a = associative: sort by the values, but keep each key attached to its value (for associative arrays).
  • r = reverse: descending order, from big to small.
  • no letter: sort() sorts by value in ascending order and gives new keys 0, 1, 2 …
  • ⁦k = key⁩ (): بەپێی ڕیز بکە.
  • ⁦a = associative⁩ (پەیوەندیدار): بەپێی ڕیز بکە، بەڵام هەر بە بهێڵەوە (بۆ ).
  • ⁦r = reverse⁩ (پێچەوانە): ، لە گەورەوە بۆ بچووک.
  • بێ پیت: sort() بەپێی بە ڕیز دەکات و نوێی 0، 1، 2 … دەدات.
A name builder: first choose a letter for WHAT to sort by: no letter (by value, keys become 0, 1, 2), a (associative: by value, each key stays with its value) or k (by key). Then choose no letter (ascending, small to big) or r (reverse: descending, big to small). Then add sort. The six names: sort, asort, ksort, rsort, arsort, krsort.دروستکەری ناو: سەرەتا پیتێک بۆ «بەپێی چی» هەڵبژێرە: بێ پیت (بەپێی نرخ، کلیلەکان دەبنە 0، 1، 2)، a (پەیوەندیدار: بەپێی نرخ، هەر کلیلێک لەگەڵ نرخەکەی دەمێنێتەوە) یان k (بەپێی کلیل). پاشان بێ پیت (ڕیزی بەرزبوونەوە، لە بچووکەوە بۆ گەورە) یان r (پێچەوانە: ڕیزی نزمبوونەوە، لە گەورەوە بۆ بچووک). پاشان sort زیاد بکە. شەش ناوەکە: sort، asort، ksort، rsort، arsort، krsort.
Build any sort function name from its letters.ناوی هەر لە پیتەکانییەوە دروست بکە.CêIT
Quick checkپشکنینی خێراWhat does the "a" in asort() stand for?پیتی «a» لە asort()دا کورتکراوەی چییە؟

sort() and rsort(): sorting listssort() و rsort(): ڕیزکردنی لیستەکان

sort($array) puts the values in ascending order; rsort($array) puts them in descending order. Both give the elements new indexes 0, 1, 2 …, which makes them perfect for indexed arrays. The lab prints the sorted arrays with a for loop and count().

sort($array) بە ڕیز دەکات؛ rsort($array) بە . هەردووکیان نوێی 0، 1، 2 … دەدەنە ، کە وایان لێدەکات بۆ تەواو گونجاو بن. تاقیگەکە ڕیزکراو بە و count() چاپ دەکات.

Lab 4, Example 10: sort an array alphabeticallyتاقیگەی 4، نموونەی ⁦10⁩: ڕیزکردنی ڕیزەکێک بەپێی ئەلفوبێPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

sort() changes $cars itself: BMW, Toyota, Volvo.

sort() خودی $cars دەگۆڕێت: BMW، Toyota، Volvo.

Lineدێڕی 4

count() returns 3, the number of elements.

count() 3 دەگەڕێنێتەوە، واتە ژمارەی .

Lineدێڕی 5-8

Print index 0, 1, 2 of the sorted array.

0، 1، 2ی ڕیزکراوەکە چاپ بکە.

Lab 4, Example 11: sorting numbersتاقیگەی 4، نموونەی ⁦11⁩: ڕیزکردنی ژمارەکانPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Numbers are compared as numbers, so 11 comes after 6 (not after 2, as it would in a dictionary).

ژمارەکان وەک ژمارە بەراورد دەکرێن، بۆیە ⁦11⁩ دوای 6 دێت (نەک دوای 2 وەک لە فەرهەنگدا).

Lab 4, Example 12: descending alphabetical orderتاقیگەی 4، نموونەی ⁦12⁩: ڕیزبەندیی ئەلفوبێیی، لە Z بۆ APHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

rsort(): the same as sort(), but from Z to A.

rsort(): وەک sort()، بەڵام لە Z بۆ A.

Quick checkپشکنینی خێرا$a = ["x" => 3, "y" => 1]; sort($a); What are the keys of $a now?$a = ["x" => 3, "y" => 1]; sort($a); ئێستا $a چین؟

asort() and arsort(): by value, keeping the keysasort() و arsort(): بەپێی نرخ، بە پاراستنی کلیلەکان

asort() sorts an associative array by its values in ascending order; arsort() does the same in descending order. Every key moves together with its value. The lab prints the result with foreach ($age as $x => $x_value), which gives each key in $x and each value in $x_value.

asort() بەپێی بە ڕیز دەکات؛ arsort() هەمان کار بە دەکات. هەر لەگەڵ پێکەوە دەجوڵێن. تاقیگەکە ئەنجامەکە بە foreach ($age as $x => $x_value) چاپ دەکات، کە هەر دەخاتە $x و هەر دەخاتە $x_value.

Lab 4, Example 13: sort by value, ascendingتاقیگەی 4، نموونەی ⁦13⁩: ڕیزکردن بەپێی نرخ، بەرزبوونەوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);

foreach ($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Name => age. The ages are written as strings ("35"); numbers (35) would be more natural, and sort the same way here.

ناو => تەمەن. تەمەنەکان وەک نووسراون ⁦("35")⁩؛ ژمارە ⁦(35)⁩ سروشتیتر دەبوو و لێرەدا بە هەمان شێوە ڕیز دەکرێت.

Lineدێڕی 3

asort() sorts by the values 35, 37, 43 and keeps the names.

asort() بەپێی ⁦35⁩، ⁦37⁩، ⁦43⁩ ڕیز دەکات و ناوەکان دەهێڵێتەوە.

Lineدێڕی 5-8

$x is the key (name), $x_value the value (age).

$x (ناو)، $x_value (تەمەن).

asort() where the order changesasort() کاتێک ڕیزبەندییەکە دەگۆڕێتPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];

asort($age);    // by value, smallest first, keys stay with their values

foreach ($age as $name => $years) {
    echo "$name is $years<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Unsorted: Rezan 22, Aram 19, Shilan 21.

ڕیزنەکراو: ⁦Rezan 22⁩، ⁦Aram 19⁩، ⁦Shilan 21⁩.

Lineدێڕی 4

Smallest value first; each name stays with its age.

بچووکترین یەکەم؛ هەر ناوێک لەگەڵ تەمەنەکەی دەمێنێتەوە.

arsort(): by value, descendingarsort(): بەپێی نرخ، نزمبوونەوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = ["Rezan" => 22, "Aram" => 19, "Shilan" => 21];

arsort($age);   // by value, largest first, keys stay with their values

foreach ($age as $name => $years) {
    echo "$name is $years<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Largest value first.

گەورەترین یەکەم.

Quick checkپشکنینی خێراWhich function sorts an associative array by value in descending order and keeps the keys?کام بەپێی بە ڕیز دەکات و دەپارێزێت؟

ksort() and krsort(): by keyksort() و krsort(): بەپێی کلیل

ksort() sorts an associative array by its keys in ascending order (A to Z, small to big); krsort() sorts by the keys in descending order. Again every value stays with its key.

ksort() بەپێی بە ڕیز دەکات (A تا Z، بچووک بۆ گەورە)؛ krsort() بەپێی بە ڕیز دەکات. دیسان هەر لەگەڵ دەمێنێتەوە.

Lab 4, Example 14 is titled "Sort associative arrays in ascending order, according to the key", but its code calls krsort(), which sorts the keys in descending order:

تاقیگەی 4، نموونەی ⁦14⁩ ناونیشانی « بە ، بەپێی »ـە، بەڵام کۆدەکەی krsort() بانگ دەکات، کە بە ڕیز دەکات:

Lab 4, Example 14 as written: krsort()تاقیگەی 4، نموونەی ⁦14⁩ وەک نووسراوە: krsort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);

foreach ($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Keys in descending order: Peter, Joe, Ben (P > J > B).

بە : Peter، Joe، ⁦Ben (P > J > B)⁩.

What the heading asks for: ksort()ئەوەی ناونیشانەکە داوای دەکات: ksort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);    // ascending by key: this matches the lab's heading

foreach ($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Keys in ascending order: Ben, Joe, Peter.

بە : Ben، Joe، Peter.

Quick checkپشکنینی خێرا$age = ["Peter" => 35, "Ben" => 37, "Joe" => 43]; krsort($age); Which name is first?$age = ["Peter" => 35, "Ben" => 37, "Joe" => 43]; krsort($age); کام ناو یەکەمە؟

Sorting changes the original arrayڕیزکردن ڕیزەکە ڕەسەنەکە دەگۆڕێت

All six functions work in place: they rearrange the array variable you pass to them, and they return only true. A common mistake is to expect a sorted array back:

هەر شەش لە شوێنی خۆیدا کار دەکەن: ئەو ڕێک دەخەنەوە کە پێیانی دەدەیت و تەنها true دەگەڕێننەوە. باو ئەوەیە چاوەڕێی گەڕانەوەی ڕیزکراو بکەیت:

sort() returns true, not an arraysort() true دەگەڕێنێتەوە، نەک ڕیزەکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$numbers = [4, 6, 2];
$sorted = sort($numbers);     // sort() changes $numbers itself

var_dump($sorted);            // bool(true): not a sorted array!
echo implode(", ", $numbers); // the original array is now sorted
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

$sorted receives true. $numbers itself is now sorted.

$sorted true وەردەگرێت. خودی $numbers ئێستا ڕیز کراوە.

Lineدێڕی 6

implode(", ", …) joins the values with commas.

implode(", ", …) بە کۆما پێکەوە دەلکێنێت.

If you still need the original order, copy the array first and sort the copy. Assigning an array to another variable makes a copy:

ئەگەر هێشتا ڕیزبەندییە ڕەسەنەکەت پێویستە، سەرەتا کۆپی بکە و کۆپییەکە ڕیز بکە. دانانی بۆ تر کۆپییەک دروست دەکات:

Sort a copy, keep the originalکۆپییەکە ڕیز بکە، ڕەسەنەکە بهێڵەوەPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$numbers = [4, 6, 2];

$sorted = $numbers;   // 1. copy the array
sort($sorted);        // 2. sort the copy

echo "Original: " . implode(", ", $numbers) . "<br>";
echo "Sorted: " . implode(", ", $sorted);
Quick checkپشکنینی خێراWhat does var_dump(sort($n)); print for $n = [3, 1, 2];?بۆ $n = [3, 1, 2];، var_dump(sort($n)); چی چاپ دەکات؟

Mixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا

Sorting compares values two at a time. Usually that is simple, but three cases can surprise you.

هەر جارە دوو بەراورد دەکات. زۆرجار ئەمە سادەیە، بەڵام سێ حاڵەت دەتوانن سەرت سوڕبهێنن.

Mixed types

جۆری تێکەڵ

PHP 8 compares a number and a numeric string as numbers, but a number and a non-numeric string as text. Text is compared character by character, and digits come before letters. So in the homework below, "1" < 22, then 22 < "Aram Karim", then "Aram Karim" < "IT":

⁦PHP 8⁩ ژمارە و ژمارەیی وەک ژمارە بەراورد دەکات، بەڵام ژمارە و ناژمارەیی وەک دەق. دەق پیت بە پیت بەراورد دەکرێت و ژمارەکان پێش پیتەکان دێن. بۆیە لە ئەرکی ماڵەوەی خوارەوەدا، "1" < 22، پاشان 22 < "Aram Karim"، پاشان "Aram Karim" < "IT":

How PHP 8 compares mixed values⁦PHP 8⁩ چۆن نرخەکانی تێکەڵ بەراورد دەکاتPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
var_dump("1" < 22);               // true: both are numbers
var_dump(22 < "Aram Karim");      // true: compared as text, "2" comes before "A"
var_dump("Aram Karim" < "IT");    // true: "A" comes before "I"
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

A numeric string and a number: compared as numbers, 1 < 22.

ژمارەیی و ژمارە: وەک ژمارە بەراورد دەکرێن، ⁦1 < 22⁩.

Lineدێڕی 3

A number and a non-numeric string: compared as text, "22" < "Aram Karim" because "2" comes before "A".

ژمارە و ناژمارەیی: وەک دەق بەراورد دەکرێن، "⁦22" < "Aram Karim⁩" چونکە "2" پێش "A" دێت.

Mixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا

Capital lettersپیتی گەورە

Normal string sorting is case-sensitive: all capital letters come before all small letters. To ignore case, pass the flags SORT_STRING | SORT_FLAG_CASE:

ئاسایی : هەموو پیتە گەورەکان پێش هەموو پیتە بچووکەکان دێن. بۆ پشتگوێخستنی قەبارەی پیت، SORT_STRING | SORT_FLAG_CASE بدە:

Upper and lower caseپیتی گەورە و بچووکPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$fruits = ["banana", "apple", "Cherry"];

sort($fruits);
echo implode(", ", $fruits) . "<br>";   // capital letters come first

sort($fruits, SORT_STRING | SORT_FLAG_CASE);
echo implode(", ", $fruits);            // ignore upper and lower case
Quick checkپشکنینی خێرا$a = ["banana", "apple", "Cherry"]; sort($a); What is $a[0] now?$a = ["banana", "apple", "Cherry"]; sort($a); ئێستا $a[0] چییە؟

Mixed values, capital letters and number stringsنرخەکانی تێکەڵ، پیتی گەورە و ژمارە لەناو زنجیرەنووسەدا

Numbers stored as stringsژمارەی هەڵگیراو وەک زنجیرەنووسە

Strings that look like numbers ("10", "9", "100") are compared as numbers by default. If you want dictionary order instead, use SORT_STRING:

کە وەک ژمارە دەردەکەون (⁦"10⁩"، "9"، "⁦100"⁩) بە شێوەی بنەڕەتی وەک ژمارە بەراورد دەکرێن. ئەگەر ڕیزبەندیی فەرهەنگیت دەوێت، SORT_STRING بەکاربهێنە:

Numeric stringsزنجیرەنووسەکانی ژمارەییPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$codes = ["10", "9", "100"];

sort($codes);                  // numeric strings are compared as numbers
echo implode(", ", $codes) . "<br>";

sort($codes, SORT_STRING);     // compare them as text, letter by letter
echo implode(", ", $codes);

Worked homework: sorting the personal-information arrayئەرکی ماڵەوەی چارەسەرکراو: ڕیزکردنی ڕیزەکی زانیاریی کەسی

Questions 4 and 5 of the associative-array homework (questions 1–3 are in Arrays in PHP) sort the personal-information array. The question texts were not saved; they are rebuilt here from the answers, with neutral data.

پرسیاری 4 و 5ی ئەرکی ماڵەوەی (پرسیاری ⁦1–3⁩ لە ڕیزەکەکان لە PHPـدادان) زانیاریی کەسی ڕیز دەکەن. دەقی پرسیارەکان پاشەکەوت نەکراون؛ لێرەدا لە چارەسەرەکانەوە دووبارە دروست کراونەتەوە، بە زانیاریی بێلایەن.

ExerciseڕاهێنانQuestion 4: asort()پرسیاری 4: asort()

Sort the personal-information array (ID, Name, Age, Department, City) in ascending order by value, keeping the keys, and print every key and value.

زانیاریی کەسی (ID، Name، Age، Department، City) بە بەپێی ڕیز بکە، بە پاراستنی و هەموو و چاپ بکە.

Show answerوەڵامەکە پیشان بدە
Solution to question 4چارەسەری پرسیاری 4PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$personalInformation = array(
    'ID' => "1",
    'Name' => "Aram Karim",
    'Age' => 22,
    'Department' => "IT",
    'City' => "Slemani"
);

echo "Question 4's Answer: <br />";
asort($personalInformation);
foreach ($personalInformation as $key => $value) {
    echo $key . ": " . $value . " — ";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

asort(): by value, ascending, keys kept.

asort(): بەپێی ، بەرزبوونەوە، دەمێننەوە.

Why this order? The values are mixed: the string "1", the integer 22 and three non-numeric strings. As shown above, "1" < 22 as numbers, 22 < "Aram Karim" as text (digits before letters), and then the strings in alphabetical order: "Aram Karim" < "IT" < "Slemani". So "from small to big" only partly describes the result. The student also wrote the function name as "absort()" in the comment; the real name is asort().

بۆچی ئەم ڕیزبەندییە؟ تێکەڵن: "1"، ⁦22⁩ و سێ ناژمارەیی. وەک لە سەرەوە پیشان درا، "⁦1" < 22⁩ وەک ژمارە، ⁦22 < "Aram Karim⁩" وەک دەق (ژمارەکان پێش پیتەکان)، پاشان بە ڕیزی ئەلفوبێ: "⁦Aram Karim" < "IT" < "Slemani⁩". بۆیە «لە بچووکەوە بۆ گەورە» تەنها بەشێک لە ئەنجامەکە وەسف دەکات. خوێندکارەکە لە ناوی بە «⁦absort()⁩» نووسیبوو؛ ناوە ڕاستەکەی asort()ـە.

ExerciseڕاهێنانQuestion 5: krsort()پرسیاری 5: krsort()

Sort the same array in descending order by key and print it.

هەمان بە بەپێی ڕیز بکە و چاپی بکە.

Show answerوەڵامەکە پیشان بدە
Solution to question 5چارەسەری پرسیاری 5PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$personalInformation = array(
    'ID' => "1",
    'Name' => "Aram Karim",
    'Age' => 22,
    'Department' => "IT",
    'City' => "Slemani"
);

echo "Question 5's Answer: <br />";
krsort($personalInformation);
foreach ($personalInformation as $key => $value) {
    echo $key . ": " . $value . " — ";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

krsort(): by key, descending. N > I > D > C > A, so Name comes first and Age last.

krsort(): بەپێی ، نزمبوونەوە. ⁦N > I > D > C > A⁩، بۆیە Name یەکەم دێت و Age کۆتایی.

The student explained: the r in krsort() means reverse and the k means key, so the sorting is done by the keys. "N" comes after "I", "D", "C" and "A", which is why "Name" is printed first, then "ID", and so on. In the original file this question sorted the array that question 4 had already sorted; with krsort() the result is the same either way, because it only looks at the keys.

خوێندکارەکە ڕوونی کردەوە: r لە krsort()دا واتە reverse (پێچەوانە) و k واتە key، بۆیە بەپێی دەکرێت. «N» دوای «I»، «D»، «C» و «A» دێت، هەر بۆیە «Name» یەکەم چاپ دەکرێت، پاشان «ID» و بەم شێوەیە. لە فایلە ڕەسەنەکەدا ئەم پرسیارە ئەو ڕیز دەکرد کە پرسیاری 4 پێشتر ڕیزی کردبوو؛ بە krsort() ئەنجامەکە هەر وەک یەکە، چونکە تەنها سەیری دەکات.

Common mistakesهەڵە باوەکان

  • sort() on an associative array: the keys are replaced by 0, 1, 2. Use asort() / arsort() (by value) or ksort() / krsort() (by key).
  • $sorted = sort($a);: $sorted becomes true. Sort functions change $a itself.
  • Thinking the a in asort means "ascending": it means associative (keys kept).
  • Choosing krsort() when you want ascending keys: use ksort(). The r always means reverse (descending).
  • Forgetting that the original order is lost: copy the array first if you need it later.
  • Surprised by capital letters or mixed types: "Cherry" comes before "apple"; numbers come before letters. Use sort flags or keep one type per array.
  • Using a for loop to print a sorted associative array: its keys are not 0, 1, 2. Use foreach ($a as $key => $value).
  • sort() لەسەر : بە 0، 1، 2 دەگۆڕدرێن. asort() / arsort() (بەپێی ) یان ksort() / krsort() (بەپێی ) بەکاربهێنە.
  • $sorted = sort($a);: $sorted دەبێتە true. خودی $a دەگۆڕن.
  • وا زانین کە a لە asortدا واتە «ascending»: واتە associative ( دەمێننەوە).
  • هەڵبژاردنی krsort() کاتێک بەرزبوونەوەت دەوێت: ksort() بەکاربهێنە. r هەمیشە واتە پێچەوانە (نزمبوونەوە).
  • لەبیرچوونی ئەوەی ڕیزبەندیی ڕەسەن ون دەبێت: ئەگەر دواتر پێویستت پێیەتی، سەرەتا کۆپی بکە.
  • سەرسووڕمان لە پیتی گەورە یان جۆری تێکەڵ: "Cherry" پێش "apple" دێت؛ ژمارەکان پێش پیتەکان دێن. ئاڵاکانی بەکاربهێنە یان لە هەر یەک جۆر دابنێ.
  • بەکارهێنانی بۆ چاپکردنی ڕیزکراو: 0، 1، 2 نین. foreach ($a as $key => $value) بەکاربهێنە.

Exercisesڕاهێنانەکان

Lab 4, Examples 10–14تاقیگەی 4، نموونەکانی ⁦10–14⁩

ExerciseڕاهێنانExample 10نموونەی ⁦10⁩

Lab 4, Example 10: sort the array ("Volvo", "BMW", "Toyota") alphabetically and print each car on its own line using count() and a for loop.

تاقیگەی 4، نموونەی ⁦10⁩: (⁦"Volvo⁩"، "BMW"، "⁦Toyota"⁩) بەپێی ئەلفوبێ ڕیز بکە و هەر ئۆتۆمبێلێک لە دێڕێکی جیادا بە count() و چاپ بکە.

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
ExerciseڕاهێنانExample 11نموونەی ⁦11⁩

Lab 4, Example 11: sort the numbers (4, 6, 2, 22, 11) in ascending order and print them.

تاقیگەی 4، نموونەی ⁦11⁩: ژمارەکانی (⁦4⁩، 6، 2، ⁦22⁩، ⁦11⁩) بە ڕیز بکە و چاپیان بکە.

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
    echo $numbers[$x];
    echo "<br>";
}
ExerciseڕاهێنانExample 12نموونەی ⁦12⁩

Lab 4, Example 12: print the same cars in descending alphabetical order.

تاقیگەی 4، نموونەی ⁦12⁩: هەمان ئۆتۆمبێلەکان بە ڕیزبەندیی ئەلفوبێیی پێچەوانە (لە Z بۆ A) چاپ بکە.

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
    echo $cars[$x];
    echo "<br>";
}
ExerciseڕاهێنانExample 13نموونەی ⁦13⁩

Lab 4, Example 13: sort the associative array ("Peter" => "35", "Ben" => "37", "Joe" => "43") in ascending order according to the value, and print "Key=…, Value=…" for each element.

تاقیگەی 4، نموونەی ⁦13⁩: (⁦"Peter" => "35⁩"، "⁦Ben" => "37⁩"، "⁦Joe" => "43"⁩) بە بەپێی ڕیز بکە و بۆ هەر «Key=…, Value=…» چاپ بکە.

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);

foreach ($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
ExerciseڕاهێنانExample 14نموونەی ⁦14⁩

Lab 4, Example 14: sort the same array in ascending order according to the key. (The lab's code uses krsort(); what does it print, and which function matches the task?)

تاقیگەی 4، نموونەی ⁦14⁩: هەمان بە بەپێی ڕیز بکە. (کۆدی تاقیگەکە krsort() بەکاردەهێنێت؛ چی چاپ دەکات و کام لەگەڵ ئەرکەکە دەگونجێت؟)

Show answerوەڵامەکە پیشان بدە
Solution with ksort()چارەسەر بە ksort()PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);    // ascending by key: this matches the lab's heading

foreach ($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}

The lab's krsort() prints Peter, Joe, Ben (descending keys). The task asks for ascending keys, which is ksort(): Ben, Joe, Peter.

krsort()ـی تاقیگەکە Peter، Joe، Ben چاپ دەکات ( نزمبوونەوە). ئەرکەکە داوای بەرزبوونەوە دەکات، کە ksort()ـە: Ben، Joe، Peter.

Exercisesڕاهێنانەکان

More practiceڕاهێنانی زیاتر

ExerciseڕاهێنانNames both waysناوەکان بە هەردوو ئاڕاستە

Store five names in an array. Print them from A to Z, then from Z to A.

پێنج ناو لە هەڵبگرە. لە A تا Z چاپیان بکە، پاشان لە Z تا A.

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$names = ["Shilan", "Aram", "Rezan", "Dilan", "Karwan"];

sort($names);
echo "A to Z: " . implode(", ", $names) . "<br>";

rsort($names);
echo "Z to A: " . implode(", ", $names);
ExerciseڕاهێنانCheapest firstهەرزانترین یەکەم

A shop stores item => price. Print the items from cheapest to most expensive, then in alphabetical order of the item names. Keep each item with its price.

دوکانێک کاڵا => نرخ هەڵدەگرێت. کاڵاکان لە هەرزانترینەوە بۆ گرانترین چاپ بکە، پاشان بە ڕیزی ئەلفوبێی ناوی کاڵاکان. هەر کاڵایەک لەگەڵ نرخەکەی بهێڵەوە.

Hintئاماژە

By value with the keys kept: asort(). By key: ksort().

بەپێی و پاراستنی : asort(). بەپێی : ksort().

Show answerوەڵامەکە پیشان بدە
SolutionچارەسەرPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$prices = ["Tea" => 1000, "Rice" => 3000, "Sugar" => 2500, "Bread" => 500];

asort($prices);
echo "Cheapest first:<br>";
foreach ($prices as $item => $price) {
    echo "$item: $price<br>";
}

ksort($prices);
echo "By name:<br>";
foreach ($prices as $item => $price) {
    echo "$item: $price<br>";
}

Summary and reviewکورتە و پێداچوونەوە

  • Six functions sort arrays: sort, rsort, asort, arsort, ksort, krsort.
  • Letters: k = by key, a = associative (by value, keys kept), r = reverse (descending). No k/a: by value with new keys.
  • Use sort/rsort for indexed lists; asort/arsort/ksort/krsort for associative arrays.
  • All six sort in place and return true; copy first if you need the original order.
  • Numbers sort numerically; strings sort character by character, capital letters first; mixed types follow PHP 8 comparison rules.
  • Print a sorted associative array with foreach ($a as $key => $value).
  • شەش ڕیز دەکەن: sort، rsort، asort، arsort، ksort، krsort.
  • پیتەکان: k = بەپێی ، a = پەیوەندیدار (بەپێی ، دەمێننەوە)، r = پێچەوانە (نزمبوونەوە). بێ k/a: بەپێی بە نوێ.
  • sort/rsort بۆ ؛ asort/arsort/ksort/krsort بۆ .
  • هەر شەشیان لە شوێنی خۆیدا ڕیز دەکەن و true دەگەڕێننەوە؛ ئەگەر ڕیزبەندیی ڕەسەنت پێویستە سەرەتا کۆپی بکە.
  • ژمارەکان وەک ژمارە ڕیز دەکرێن؛ پیت بە پیت، پیتە گەورەکان یەکەم؛ جۆری تێکەڵ پەیڕەوی یاساکانی بەراوردکردنی ⁦PHP 8⁩ دەکات.
  • ڕیزکراو بە foreach ($a as $key => $value) چاپ بکە.
Review questionsپرسیارەکانی پێداچوونەوە
What is the difference between asort() and ksort()?جیاوازیی نێوان asort() و ksort() چییە؟

asort() sorts by the values, ksort() by the keys. Both keep each key with its value, and both are ascending.

asort() بەپێی ڕیز دەکات، ksort() بەپێی . هەردووکیان هەر لەگەڵ دەهێڵنەوە و هەردووکیان بەرزبوونەوەن.

Why is sort() a bad choice for ["Aram" => 19, "Rezan" => 22]?بۆچی sort() هەڵبژاردنێکی خراپە بۆ ["Aram" => 19, "Rezan" => 22]؟

It replaces the names with 0 and 1, so you can no longer tell whose age is whose.

ناوەکان بە 0 و 1 دەگۆڕێت، بۆیە ئیتر نازانیت کام تەمەن هی کێیە.

After $a = [4, 6, 2]; rsort($a);, what is in $a?دوای $a = [4, 6, 2]; rsort($a);، چی لەناو $aدایە؟

[0 => 6, 1 => 4, 2 => 2]. (rsort() needs a variable: rsort([4, 6, 2]) is an error.)

[0 => 6, 1 => 4, 2 => 2]. (rsort() پێویستی بە هەیە: rsort([4, 6, 2]) .)

Lab 4, Example 14 says "ascending, according to the key" but uses krsort(). Which is right?تاقیگەی 4، نموونەی ⁦14⁩ دەڵێت «بەرزبوونەوە، بەپێی کلیل» بەڵام krsort() بەکاردەهێنێت. کامیان ڕاستە؟

Ascending by key is ksort(); krsort() is descending. The heading and the code disagree.

بەرزبوونەوە بەپێی ksort()ـە؛ krsort() نزمبوونەوەیە. ناونیشان و کۆدەکە یەک ناگرنەوە.

Why does "Cherry" come before "apple" after sort()?بۆچی دوای sort() "Cherry" پێش "apple" دێت؟

String sorting is case-sensitive and capital letters come before small letters. Use SORT_STRING | SORT_FLAG_CASE to ignore case.

و پیتە گەورەکان پێش پیتە بچووکەکان دێن. بۆ پشتگوێخستنی قەبارەی پیت SORT_STRING | SORT_FLAG_CASE بەکاربهێنە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

PHP Superglobalsسووپەرگلۆباڵەکانی PHP

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.