arrayutils
  • Home
  • 📖How to use?
    • âš¡Installation
  • 📖Methods
    • âš¡Static method
      • static from()
      • static of()
      • static mapToArray()
    • âš¡Generic method
      • join()
      • every()
      • some()
      • reduce()
        • reduceRight()
      • sum()
      • pop()
      • shift()
      • includes()
      • keyExists()
      • indexOf()
      • find()
        • findIndex()
      • first()
        • keyFirst()
      • last()
        • keyLast()
      • random()
        • keyRandom()
      • splice()
    • âš¡Chain method
      • chunk()
      • column()
      • combine()
      • concat()
        • concatSoft()
      • countValues()
      • diff()
        • diffAssoc()
        • diffKey()
      • fill()
        • fillKeys()
      • filter()
      • flat()
        • flatMap()
      • flip()
      • forEach()
      • intersect()
        • intersectAssoc()
        • intersectKey()
      • keys()
      • map()
        • mapAssoc()
        • mapKey()
      • pad()
      • push()
      • replace()
      • reverse()
      • slice()
      • sort()
        • sortKey()
      • unique()
      • unshift()
      • values()
  • 📖Suffixes
    • âš¡Suffix - From
    • âš¡Suffix - As
  • links
    • 📌Github repo
    • 📌Packagist project
    • 📌Poggit project
Powered by GitBook
On this page
  • Syntax
  • Parameter
  • Return value
  • Prefixing
  • References
  1. 📖Methods
  2. ⚡Chain method
  3. sort()

sortKey()

The sortKey() method sort an array by keys using a function or default sort function

Example.php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from([
    "orange" => 1, 
    "banana" => 2, 
    "apple" => 3,
    "raspberry" => 4, 
    "kiwi" => 5]);


$arrayUtils->sortKey();
// expected output: [
//  "apple" => 3,
//  "banana" => 2, 
//  "kiwi" => 5,
//  "orange" => 1, 
//  "raspberry" => 4
//]

$arrayUtils->sortKey(function($a, $b){ return strcmp($a, $b) * -1; })
// expected output: [
//  "raspberry" => 4,
//  "orange" => 1, 
//  "kiwi" => 5,
//  "banana" => 2, 
//  "apple" => 3
//]

Syntax

$arrayUtils->sortKey(?callable $callback = null) : ArrayUtils;

Parameter

  • A function to compare element for sort, taking two arguments:

    • $a The comparison target A

    • $b The comparison target B

    Default is NULL, If is null, Sort by default sort function.

Return value

  • A sorted array.

Prefixing

$arrayUtils->sortKeyAs(?callable $callback = null) : array;
ArrayUtils::sortKeyFrom(iterable $from, ?callable $callback = null) : ArrayUtils;
ArrayUtils::sortKeyFromAs(iterable $from, ?callable $callback = null) : array;

References

Previoussort()Nextunique()

Last updated 4 years ago

$callback

PHP: ksort - Manual
Logo
PHP: uksort - Manual
Logo