Logo

Programming-Idioms

# 290 Sort sublist
Sort the part of the list items from index i (included) to index j (excluded), in place, using the comparator c.

Elements before i and after j must remain unchanged.
New 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
items.setRange(i, j, items.sublist(i, j)..sort(c));
sub := items[i:j]
sort.Slice(sub, func(a, b int) bool {
	return c(sub[a], sub[b])
})
import "slices"
slices.SortFunc(items[i:j], c)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Comparator<T> c = new Comparator<T>()
{
	@Override
	public int compare(T o1, T o2)
	{
		if(o1 > o2) {
			return 1;
		} else if(o1 == o2) {
			return 0;
		} else {
			return -1;
		}
	}
};
Collections.sort(items.subList(i, j), c);
@items[$i..$j] = sort $c @items[$i..$j];
import functools
items[i:j] = sorted(items[i:j], key=functools.cmp_to_key(c))
items[i:j] = sorted(items[i:j], key=c)
items[i..j] = items[i..j].sort_by{|el| c(el) }
items[i..j].sort_by(c);