sort an array of values code example

Example 1: sort array php

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange

Example 2: javascript sort by id

elems.sort((a, b) => a.id - b.id);

Example 3: php sort hight to low

$letters=array("b","a","c");
arsort($letters); //referse sort an array ("c","a","b")

Example 4: array sort js

arr = ['width', 'score', done', 'neither' ]
arr.sort() // results to ["done", "neither", "score", "width"]

arr.sort((a,b) => a.localeCompare(b)) 
// if a-b (based on their unicode values) produces a negative value, 
// a comes before b, the reverse if positive, and as is if zero

//When you sort an array with .sort(), it assumes that you are sorting strings
//. When sorting numbers, the default behavior will not sort them properly.
arr = [21, 7, 5.6, 102, 79]
arr.sort((a, b) => a - b) // results to [5.6, 7, 21, 79, 102]
// b - a will give you the reverse order of the sorted items 

//this explnation in not mine

Example 5: javascript sort

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));

Example 6: array sort php

// array sort php
$room_details = array(
      "2020-09-27": [
                {
                    "content": "how are you",
                    "detail_id": "1",
                    "time": "17:57:28",
                    "chat_time": "2020-09-24 17:57:28",
                    "width": "0",
                    "height": "0",
                    "type": "1",
                    "distance_time": "26 days ago",
                    "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
                    "position": 2
                },
                {
                    "content": "I am fine, thanks",
                    "detail_id": "2",
                    "time": "17:57:45",
                    "chat_time": "2020-09-24 17:57:45",
                    "width": "0",
                    "height": "0",
                    "type": "1",
                    "distance_time": "26 days ago",
                    "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
                    "position": 2
                },
      ],
	 "2020-09-24": [
                {
                    "content": "how are you",
                    "detail_id": "1",
                    "time": "17:57:28",
                    "chat_time": "2020-09-24 17:57:28",
                    "width": "0",
                    "height": "0",
                    "type": "1",
                    "distance_time": "26 days ago",
                    "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
                    "position": 2
                },
                {
                    "content": "I am fine, thanks",
                    "detail_id": "2",
                    "time": "17:57:45",
                    "chat_time": "2020-09-24 17:57:45",
                    "width": "0",
                    "height": "0",
                    "type": "1",
                    "distance_time": "26 days ago",
                    "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
                    "position": 2
                },
      ],
);

sort($room_details);

// result
// array sort php
$room_details = array(
      "2020-09-24": [
                ...
      ],
	 "2020-09-27": [
               ...
      ],
);

Tags:

Php Example