Sorting can be tricky, especially if your list

Then we define the implementation of the interface

`List`

contains object wrappers ( `Byte`

, `Integer`

, `Short`

, `Long`

, `Double`

, `Float`

) rather than primitive numeric data types. I suggest using a method that can be very useful for solving simple problems or answering an interviewer’s question. First, let's choose what we will fill our `List`

. In this example I will use a list of graph edges ( `Edges`

) from a simple data structure `Graph`

:
```
// Очень простой класс Edge (рёбро графа)
public class Edge {
public Vertex src;
public Vertex dst;
public double cost;
// создание ребра между двух вершин
Edge(Vertex s, Vertex d, double c) {
src = s;
dst = d;
cost = c;
}
}
// Список рёбер
Edge[] edges = graph.getEdges();
```

What is a graph? In discrete mathematics, a graph is defined as a pair of sets ( Probably, for those who are not accustomed to mathematical abstractions, such a definition will not be very clear, so let’s say it more simply:
The vertices of such a graph are |

`java.util.Comparator`

:
```
class SortByCost implements Comparator<Edge> {
public int compare(Edge a, Edge b) {
if ( a.cost < b.cost ) return -1;
else if ( a.cost == b.cost ) return 0;
else return 1;
}
}
```

In this example, we will sort the edges of the graph by their weight, or by the distance from the vertex *(source) to the vertex***src***(destination). Finally, we use the standard method***dst**`java.util.Arrays.sort ()`

:
`Arrays.sort(edges, new SortByCost())`

Now our sheet filled with objects `Edges`

is sorted in ascending order (from smallest to largest). *The author of the solution is Ethan Arrowood.*
GO TO FULL VERSION