Logo

Programming-Idioms

Sort the elements of the list (or array-like collection) items in ascending order of x.p, where p is a field of the type Item of the objects in items.
Implementation
Scala

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Scala implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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;
	}
});
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)
	sort.Sort(sorter)
}
import java.util.Collections;
import java.util.Comparator;
Collections.sort(items, new Comparator<Item>(){
	@Override
	public int compare(Item a, Item b){
		return a.p - b.p;
	}
});
items.sort(function(a,b) {
  return compareFieldP(a.p, b.p);
});
@items = sort { $a->{p} cmp $b->{p} } @items;
items = sorted(items, key=lambda x: x.p)
items.sort_by(|a,b| a.p.cmp(&b.p));
import std.algorithm.sorting;
sort!("a.p < b.p")(items);
items.sort((a, b) => Comparable.compare(a.p, b.p));
items.sort((a, b) => (a.p).compareTo(b.p));
uses fgl;
type

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

var items: TItems;

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

begin
  items := TItems.Create;
  // Add items here.
  items.Sort(@compare);
end.  
	
sort_by_birth(ListOfMaps) ->
  lists:sort(
    fun(A, B) ->
      maps:get(birth, A, undefined) =< maps:get(birth, B, undefined)
    end, ListOfMaps).

sort_by_birth(ListOfRecords) -> lists:keysort(#item.birth, ListOfRecords).
sortBy (comparing p) items
items.sort_by(&:p)
Enum.sort_by(items, &(&1.p))
table.sort(items, function(a,b)
	if a.p < b.p then return true end
end)
#include <algorithm>
std::sort(begin(items), end(items), [](const auto& a, const auto& b) { return a.p < b.p; });
using System.Collections.Generic;
System.Array.Sort(items, Comparer<Item>.Create((a, b) => a.p - b.p));
with Ada.Containers.Vectors;
use Ada.Containers;
declare
   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;
begin
   Sort (Items);
end;
function cmp($a, $b)
{
    if ($a->p == $b->p) {
        return 0;
    }

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

usort($items, 'cmp');
import "sort"
less := func(i, j int) bool {
	return items[i].p < items[j].p
}
sort.Slice(items, less)
items.sort_by_key(|x| x.p);
#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)
import qualified Data.List as List
List.sortOn p items
(sort #'< items :key #'p)
import java.util.Comparator;
items.stream().sorted(Comparator.comparing(x -> x.p))
items.sortedBy { it.p }
from operator import attrgetter
items = sorted(items, key=attrgetter('p'))
@import Foundation;
[items sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"p" ascending:YES]]]
items.sort { x -> x.p }
items.sort { x, y -> x.p <=> y.p }
items.sortBy(_.x)
#include <algorithm>
struct {
    bool operator()(const Item &lhs, const Item &rhs) const { return lhs.p < rhs.p; }
} custom_sort;

std::sort(begin(items), end(items), custom_sort);
(define-struct item (p x y) #:transparent)
(define items (list (item 1 2 3) (item 0 0 0) (item 5 2 1)))
(sort items < #:key item-p)
using System.Linq;
items.OrderBy(x => x.p)
Imports System.Linq
items.OrderBy(Function(x) x.p)
Imports System.Collections.Generic
System.Array.Sort(items, Comparer(Of Item).Create(Function(a, b) a.p - b.p))
items sorted: [:x :y | x p <= y p].
import "slices"
import "cmp"
compare := func(a, b Item) int {
	return cmp.Compare(a.p, b.p)
}
slices.SortFunc(items, compare)