swap<T> static method

List<T> swap<T>(
  1. Iterable<T> list,
  2. int i,
  3. int j
)

Returns a new list with the elements at i and j swapped.

Implementation

static List<T> swap<T>(Iterable<T> list, int i, int j) {
  final source = list.toList();
  if (i < 0 || i >= source.length) {
    throw RangeError.index(i, source, 'i');
  }
  if (j < 0 || j >= source.length) {
    throw RangeError.index(j, source, 'j');
  }
  final tmp = source[i];
  source[i] = source[j];
  source[j] = tmp;
  return source;
}