Last active
October 26, 2019 19:53
-
-
Save unitycoder/80f76a58ebe535658fd0 to your computer and use it in GitHub Desktop.
Sort System.Array of custom Struct with CompareTo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.Sort(tiles, new Comparison<YourThing>((a, b) => b.x.CompareTo(a.x))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// declare | |
PointData[] pointArray; | |
// init | |
pointArray = new PointData[masterPointCount]; | |
// fill | |
pointArray[rowCount].vertex = new Vector3(x,y,z); | |
pointArray[rowCount].uv = new Vector2(x,y); | |
// sort | |
System.Array.Sort<PointData>(pointArray, (a,b) => (corner-a.vertex).sqrMagnitude.CompareTo((corner-b.vertex).sqrMagnitude)); | |
// struct | |
using UnityEngine; | |
public struct PointData | |
{ | |
public Vector3 vertex; | |
public Vector2 uv; | |
public int indice; | |
public Color color; | |
public Vector3 normal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment