Change language

PHP Sort () Function

Syntax:
bool sort ($array, sorting_type)
Parameters:
  • $array -The parameter specifies the array that we want to sort. This is a required parameter
  • sorting_type -this is an optional parameter. There are 6 types of sorting, which are described below:
    • SORT_REGULAR- when we pass 0or SORT_REGULARin sorting_type , the elements in the array are compared as usual.
    • SORT_NUMERIC - when we pass 1or SORT_NUMERICin sorting_type , the elements in the array are compared numerically
    • SORT_STRING - When we pass 2or SORT_STRINGin the sorting_typeparameter, the elements in the array are compared by string
    • SORT_LOCALE_STRING - When we pass 3or SORT_LOCALE_STRINGin sorting_type , the items in the array are compared as strings based on the current locale
    • SORT_NATURAL- when we pass 4or SORT_NATURALin sorting_type , the elements in the array are compared as strings using natural ordering
    • SORT_FLAG_CASE- When we pass 5or SORT_FLAG_CASEin the sorting_typeparameter, the elements in the array are compared as strings. Items are treated as case insensitive and then compared. Can be used with | (bitwise operator) with SORT_NATURALor SORT_STRING .
  • Returned value: returns a Boolean value, TRUE on success and False on error. It sorts the original array in ascending order, which is passed as a parameter.Examples:
    Input: $array = [3, 4, 1, 2] Output: Array ([0 ] = > 1 [1] = > 2 [2] = > 3 [3] = > 4) Input: $array = ["engineer2", "raj1", "striver3", "coding4"] Output : Array ([0] = > coding4 [1] = > engineer2 [2] = > raj1 [3] = > striver3)
    The following programs illustrate the sort() function in PHP: Program 1:A program to demonstrate the use of the sort() function.
    // PHP program to demonstrate the use of the sort() function  $array = array (3, 4, 2, 1);  
    // sort the function sort ( $array );  
    // prints the sorted array print_r ( $array ); ?>
    Output:
    Array ([0] = > 1 [1] = > 2 [2] = > 3 [3] = > 4)
    Program 2: A program to demonstrate the use of the sort() function to sort a string in a case sensitive manner.
    // PHP program to demonstrate the use of the sort() function
    // sort the string case sensitive $array = array ( "engineer" , "Raj" , "striver" , "coding" , "RAj" );  
    // sort functions, sort the string case sensitive sort ( $array , SORT_STRING);  
    // prints the sorted array print_r ( $array ); ?>
    Output:
    Array ([0] = > RAj [1] = > Raj [2] = > coding [3] = > engineer [4] = > striver)
    Program 3:A program to demonstrate the use of the sort() function to sort a string in a case insensitive manner.
    // PHP program to demonstrate usage
    // sort() function sorts the string // case insensitive $array = array ( "engineer" , "Raj" , "striver" ,  "coding" , "RAj" );  
    // sort the function, sort
    // string is case insensitive sort ( $array , SORT_STRING | SORT_FLAG_CASE);  
    // prints the sorted array print_r ( $array ); ?>
    Output:
    Array ([0] = > coding [1] = > engineer [2] = > Raj [3] = > RAj [4] = > striver)
    Link :
    http://php.net/manual/en/function.sort. php

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically