I have a question about c# List Sort.

Asked 2 years ago, Updated 2 years ago, 43 views

The information is in the list. [0] [0] [1] [2] [3] ......

In this list, [0] [0] - X,Y [1] [1] - X,Y . . . . There are XY coordinates I'd like to do Sort with the X value. I would appreciate it if you let me know. ㅜ <

.net

2022-09-21 16:28

1 Answers

List<string> and we solved it assuming that you separated the values with commas.

using System.Collections.Generic;

public class Hello1 {
  public static void Main() {
    List<string> points = new List<string>();
        points.Add("10.5 , 50");
        points.Add("10.1, 10"); 
        points.Add("9, 30.5");

    /*Sort included a comparison function. Read the element of the list, bend it to , and then
    Convert the front part to float and compare the values. Sort by that.*/
        points.Sort((x, y) => float.Parse(x.Split(',')[0])>float.Parse(x.Split(',')[0])?1:-1);

    foreach(string str in points){
      System.Console.WriteLine(str);
    }

  }
}


2022-09-21 16:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.