struct point{ int weight, position, id; }; // . . . . . . // your code // for sorting using weight sort(points.begin(), points.end(), [] (point a, point b){ return a.weight < b.weight; }); // for sorting using positions sort(points.begin(), points.end(), [] (point a, point b){ return a.position < b.position; });