Sort a list from another list IDs
docs = docs.OrderBy(d => docsIds.IndexOf(d.Id)).ToList();
How to sort a list by another list
This isn't the most performant way, but it's probably one of the simplest. Use a sorting method that sorts based on the object's field's location in the other array:
final sortedExamples = List.from(examples)
..sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id));
This way is slightly more involved but more performant as you only need to iterate over each list once each. It involves making a map out of your list and then using that as the source of truth to rebuild a sorted list:
final ref = Map.fromIterable(examples, key: (e) => e.id, value: (e) => e);
final sortedExamples = List.from(ids.map((id) => ref[id]));
The first option is better if space is an issue, the second is better if speed is an issue.
List sort based on another list
You should be able to use a join to produce your desired output. Example using query syntax.
var orderedOptions = from option in options_list
join type in types_list
on option.Type_ID equals type.ID
orderby type.Ordering
select option;
Order list of objects by a list of ids
Sounds like you want to order by the result of IndexOf
, but to have the -1 values go to the end instead of the start. In that case, you could just process the value of the IndexOf
to, say, int.MaxValue
so it'll go at the end.
I've tidied up your code a bit to make it more readable - only the OrderBy
is different to your original code.
var outcomeIds = outcomeRequestModels
.OrderByDescending(m => m.Score)
.Select(m => m.Id)
.ToList();
groupResponseModel.Outcomes = groupOutcomes
.Select(m => Tuple.Create(m, outcomeIds.IndexOf(m.Id))
.OrderBy(m => outcomeIds.IndexOf(m.Id) == -1 ? int.MaxValue : outcomeIds.IndexOf(m.Id))
.ToList();
Or, if you don't want to call IndexOf
multiple times, you could extract the conditional statement into a method:
var outcomeIds = outcomeRequestModels
.OrderByDescending(m => m.Score)
.Select(m => m.Id)
.ToList();
groupResponseModel.Outcomes = groupOutcomes
.Select(m => Tuple.Create(m, outcomeIds.IndexOf(m.Id))
.OrderBy(m => orderByKeySelector(outcomeIds(m.Id)))
.ToList();
where orderByKeySelector
is
private static int orderByKeySelector<T>(List<T> source, T value)
{
var indexOfValue = source.IndexOf(value);
return indexOfValue == -1 ? int.MaxValue : indexOfValue;
}
Kotlin: Sort list based on another list (different objects)
The easiest approach would be to first create a mapping of the list you want to sort by. It should associate the id to the actual object.
Then you iterate the list you want to sort by and map it's values to the object you retrieved from the mapping you just created.
Here a minimal example:
// both can have more properties of course
data class Foo(val id: Int)
data class Bar(val id: Int)
val fooList = listOf(Foo(4), Foo(2), Foo(1), Foo(3))
val barList = listOf(Bar(1), Bar(2), Bar(3), Bar(4))
val idValueMap = barList.associateBy { it.id }
val sortedByOtherList = fooList.map { idValueMap[it.id] }
println(sortedByOtherList)
Result:
[Bar(id=4), Bar(id=2), Bar(id=1), Bar(id=3)]
Sort one list by another
Another LINQ-approach:
var orderedByIDList = from i in ids
join o in objectsWithIDs
on i equals o.ID
select o;
Related Topics
Catch an Exception Thrown by an Async Void Method
Ignoring Accented Letters in String Comparison
Member '≪Member Name≫' Cannot Be Accessed With an Instance Reference
Is Polymorphic Deserialization Possible in System.Text.Json
How to Display Indirect Data in Jqgrid
How Should the Viewmodel Close the Form
How to Bind to a Passwordbox in Mvvm
What Does Principal End of an Association Means in 1:1 Relationship in Entity Framework
Difference in Months Between Two Dates
Tips For Optimizing C#/.Net Programs
Check Whether an Array Is a Subset of Another
Anyone Know a Good Workaround For the Lack of an Enum Generic Constraint