Skip to main content
Better naming
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

When in doubt, I recommend consulting the documentation for the types you're using. Even using Intellisense to browse the members of List<T>List<T> would let you discover List.SortList.Sort().

This lets you sort a list using any Comparison method you choose, like for instance this one:

int SortByDistanceToMeCompareDistanceToMe(GameObject a, GameObject b) {
    float squaredRangeA = (a.transform.position - transform.position).sqrMagnitude;
    float squaredRangeB = (b.transform.position - transform.position).sqrMagnitude;
    return squaredRangeA.CompareTo(squaredRangeB);
}

Then you can simply call:

enemiesInRange.Sort(SortByDistanceToMeCompareDistanceToMe);

This is a quick & dirty solution that does strictly more vector calculations than necessary, but for a handful of enemies that won't be a problem. If you want to run this on hundreds of enemies then you'll want to pre-compute your distances first, then find the ranked/sorted order of that collection.

When in doubt, I recommend consulting the documentation for the types you're using. Even using Intellisense to browse the members of List<T> would let you discover List.Sort.

This lets you sort a list using any Comparison method you choose, like for instance this one:

int SortByDistanceToMe(GameObject a, GameObject b) {
    float squaredRangeA = (a.transform.position - transform.position).sqrMagnitude;
    float squaredRangeB = (b.transform.position - transform.position).sqrMagnitude;
    return squaredRangeA.CompareTo(squaredRangeB);
}

Then you can simply call:

enemiesInRange.Sort(SortByDistanceToMe);

This is a quick & dirty solution that does strictly more vector calculations than necessary, but for a handful of enemies that won't be a problem. If you want to run this on hundreds of enemies then you'll want to pre-compute your distances first, then find the ranked/sorted order of that collection.

When in doubt, I recommend consulting the documentation for the types you're using. Even using Intellisense to browse the members of List<T> would let you discover List.Sort().

This lets you sort a list using any Comparison method you choose, like for instance this one:

int CompareDistanceToMe(GameObject a, GameObject b) {
    float squaredRangeA = (a.transform.position - transform.position).sqrMagnitude;
    float squaredRangeB = (b.transform.position - transform.position).sqrMagnitude;
    return squaredRangeA.CompareTo(squaredRangeB);
}

Then you can simply call:

enemiesInRange.Sort(CompareDistanceToMe);

This is a quick & dirty solution that does strictly more vector calculations than necessary, but for a handful of enemies that won't be a problem. If you want to run this on hundreds of enemies then you'll want to pre-compute your distances first, then find the ranked/sorted order of that collection.

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

When in doubt, I recommend consulting the documentation for the types you're using. Even using Intellisense to browse the members of List<T> would let you discover List.Sort.

This lets you sort a list using any Comparison method you choose, like for instance this one:

int SortByDistanceToMe(GameObject a, GameObject b) {
    float squaredRangeA = (a.transform.position - transform.position).sqrMagnitude;
    float squaredRangeB = (b.transform.position - transform.position).sqrMagnitude;
    return squaredRangeA.CompareTo(squaredRangeB);
}

Then you can simply call:

enemiesInRange.Sort(SortByDistanceToMe);

This is a quick & dirty solution that does strictly more vector calculations than necessary, but for a handful of enemies that won't be a problem. If you want to run this on hundreds of enemies then you'll want to pre-compute your distances first, then find the ranked/sorted order of that collection.