This language bar is your friend. Select your favorite languages!

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.

uses fgl;

  TItem = class p: Integer; end;
  TItems = specialize TFPGObjectList<TItem>;

var items: TItems;

  function compare(const a, b: TItem): Integer;
    Result := a.p - b.p;

  items := TItems.Create;
  // Add items here.
with Ada.Containers.Vectors;
use Ada.Containers;
   function Compare_Function (Left, Right : Item) return Boolean is (Left.P < Right.P);
   package Item_Vector_Sorting is new Item_Vectors.Generic_Sorting (Compare_Function);

   use Item_Vector_Sorting;
   Sort (Items);
#include <stdlib.h>
int compareProp (const void *a, const void *b)
    return (*(const Item**)a)->p - (*(const Item**)b)->p;

qsort(items, N, sizeof(Item*), compareProp);
(sort-by :p items)
#include <algorithm>
std::sort(begin(items), end(items), [](const auto& a, const auto& b) { return a.p < b.p; });
Array.Sort(items, (a, b) => compareFunc(a.p, b.p));
import std.algorithm.sorting;
sort!("a.p < b.p")(items);
items.sort((a, b) =>, b.p));
items.sort((a, b) => (a.p).compareTo(b.p));
Enum.sort_by(items, &(&1.p))
sort_by_birth(ListOfMaps) ->
    fun(A, B) ->
      maps:get(birth, A, undefined) =< maps:get(birth, B, undefined)
    end, ListOfMaps).

sort_by_birth(ListOfRecords) -> lists:keysort(#item.birth, ListOfRecords).
import "sort"
type ItemPSorter []Item
func (s ItemPSorter) Len() int{ return len(s) }
func (s ItemPSorter) Less(i,j int) bool{ return s[i].p<s[j].p }
func (s ItemPSorter) Swap(i,j int) { s[i],s[j] = s[j],s[i] }

func sortItems(items []Item){
	sorter := ItemPSorter(items)
import "sort"
less := func(i, j int) bool {
	return items[i].p < items[j].p
sort.Slice(items, less)
items.sort { x, y -> x.p <=> y.p }
items.sort { x -> x.p }
import qualified Data.List as List
List.sortOn p items
sortBy (comparing p) items
items.sort(function(a,b) {
  return compareFieldP(a.p, b.p);
import java.util.Collections;
import java.util.Comparator;
Collections.sort(items, new Comparator<Item>(){
	public int compare(Item a, Item b){
		return a.p - b.p;
import java.util.Comparator; -> x.p))
import java.util.Arrays;
import java.util.Comparator;
Arrays.sort(items, new Comparator<Item>(){
	public int compare(Item a, Item b){
		return a.p - b.p;
items.sortedBy { it.p }
(sort #'< items :key #'p)
table.sort(items, function(a,b)
	if a.p < b.p then return true end
@import Foundation;
[items sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"p" ascending:YES]]]
function cmp($a, $b)
    if ($a->p == $b->p) {
        return 0;

    return ($a->p < $b->p) ? -1 : 1;

usort($items, 'cmp');
@items = sort { $a->{p} cmp $b->{p} } @items;
from operator import attrgetter
items = sorted(items, key=attrgetter('p'))
items = sorted(items, key=lambda x: x.p)
items.sort_by_key(|item| item.p);
case class S(x: Int)
val items = List(S(3), S(4), S(2))
items.sortBy( item: S => item.x )

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Related idioms