am a beginner in coding and still learning, i have two arrays , first one for cars and second one for the cars speeds
string[] cars = {"Volvo", "BMW","Ford", "Mazda"};
Int[] speeds = {150,130,200,110}
So that the speed of volvo is 150 , speed of bmw is 130 speed of ford is 300 ..etc
Is there a method in c# to rearrange the arrays with respect to speed from highest to lowest speed so that the arrays become as follows
String[] cars = {"ford","volvo","bmw","mazda"};
Int[] speeds = {300,150,130,110};
>Solution :
A concise way to do this is to "zip" the two arrays together into one array of tuples, sort the tuples by speed, and then split the tuples back into two arrays:
var tuples = cars.Zip(speeds, (car, speed) => (car, speed))
.OrderByDescending(tuple => tuple.speed)
.ToList();
string[] sortedCars = tuples.Select(tuple => tuple.car).ToArray();
int[] sortedSpeeds = tuples.Select(tuple => tuple.speed).ToArray();