Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting for number keys in List.SortByKey #11241

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Libraries/CoreNodes/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public static IDictionary SortByKey(IList list, IList keys)
var pairs = list.Cast<object>()
.Zip(keys.Cast<object>(), (item, key) => new { item, key });

var numberKeyPairs = pairs.Where(pair => pair.key is double || pair.key is int || pair.key is float);
var numberKeyPairs = pairs.Where(pair => pair.key is double || pair.key is int || pair.key is float || pair.key is long);
// We don't use Except, because Except doesn't return duplicates.
var keyPairs = pairs.Where(
pair =>
Expand Down
25 changes: 25 additions & 0 deletions test/DynamoCoreTests/Nodes/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,31 @@ public void SortBy_SimpleTest()
SelectivelyAssertPreviewValues("9e2c84e6-b9b8-4bdf-b82e-868b2436b865", validationData1);

}

[Test]
public void SortByKey_SimpleTest()
{
string openPath = Path.Combine(TestDirectory, @"core\list\ListSortByKey.dyn");
RunModel(openPath);

// First and last element in the list before sorting
var validationData = new []
{
"Zack",
"Ian",
"Anna",
"Neal"
};
AssertPreviewValue("aaf179d9-71a5-4c30-b603-e41cee521dff", validationData);

// First and last element in the list after sorting
var validationData1 = new object[]
{
-3, 1.6, 5, "abc"
};
AssertPreviewValue("ef7368e4-5fe4-426d-9c58-27f250a1b040", validationData1);

}
#endregion

#region Reverse Test Cases
Expand Down
262 changes: 262 additions & 0 deletions test/core/list/ListSortByKey.dyn
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"Uuid": "3c9d0464-8643-5ffe-96e5-ab1769818209",
"IsCustomNode": false,
"Description": "",
"Name": "ListSortByKey",
"ElementResolver": {
"ResolutionMap": {}
},
"Inputs": [],
"Outputs": [],
"Nodes": [
{
"ConcreteType": "Dynamo.Graph.Nodes.ZeroTouch.DSFunction, DynamoCore",
"NodeType": "FunctionNode",
"FunctionSignature": "DSCore.List.SortByKey@var[]..[],var[]..[]",
"Id": "aeff4f246990441aae2cea89cd5d2d5d",
"Inputs": [
{
"Id": "496ce45912ae4178a10d2b1eeeede378",
"Name": "list",
"Description": "list to be sorted\n\nvar[]..[]",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
},
{
"Id": "dfd6fb927b2446c0bf2014259e3bc7be",
"Name": "keys",
"Description": "list of keys, keys have to be sortable (e.g. numbers,strings)\n\nvar[]..[]",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "080467e0daa5463c806b789ce5f5424c",
"Name": "sortedList",
"Description": "type: var[]..[]",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
},
{
"Id": "0ced997499ad442e8068622bcb567bc1",
"Name": "sortedKeys",
"Description": "type: var[]..[]",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Auto",
"Description": "Sort list based on its keys\n\nList.SortByKey (list: var[]..[], keys: var[]..[]): var[]..[]"
},
{
"ConcreteType": "Dynamo.Graph.Nodes.CodeBlockNodeModel, DynamoCore",
"NodeType": "CodeBlockNode",
"Code": "[\"Zack\",\n \"Ian\",\n \"Neal\",\n \"Anna\"];",
"Id": "6a570493e8cc4b33ae1aec199b366dde",
"Inputs": [],
"Outputs": [
{
"Id": "fa6580ad9a5b4ae4b33dd5cd5422b8e6",
"Name": "",
"Description": "Value of expression at line 1",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Allows for DesignScript code to be authored directly"
},
{
"ConcreteType": "Dynamo.Graph.Nodes.CodeBlockNodeModel, DynamoCore",
"NodeType": "CodeBlockNode",
"Code": "[-3,\n 1.6,\n \"abc\",5];",
"Id": "5490581cd07147699e9b895764274ef8",
"Inputs": [],
"Outputs": [
{
"Id": "d6911c819ee942618c930b7d0b6e0d78",
"Name": "",
"Description": "Value of expression at line 1",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Allows for DesignScript code to be authored directly"
},
{
"ConcreteType": "CoreNodeModels.Watch, CoreNodeModels",
"NodeType": "ExtensionNode",
"Id": "ef7368e45fe4426d9c5827f250a1b040",
"Inputs": [
{
"Id": "73fd808a004d4e67b7d66ce8ad888a54",
"Name": "",
"Description": "Node to evaluate.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "6422c725c7d44cf28232844c451f6e47",
"Name": "",
"Description": "Watch contents.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Visualize the output of node."
},
{
"ConcreteType": "CoreNodeModels.Watch, CoreNodeModels",
"NodeType": "ExtensionNode",
"Id": "aaf179d971a54c30b603e41cee521dff",
"Inputs": [
{
"Id": "554320adf0144c6c961abb3842a9daf1",
"Name": "",
"Description": "Node to evaluate.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Outputs": [
{
"Id": "a94896972f7442319534fbcf42ffbb77",
"Name": "",
"Description": "Watch contents.",
"UsingDefaultValue": false,
"Level": 2,
"UseLevels": false,
"KeepListStructure": false
}
],
"Replication": "Disabled",
"Description": "Visualize the output of node."
}
],
"Connectors": [
{
"Start": "080467e0daa5463c806b789ce5f5424c",
"End": "554320adf0144c6c961abb3842a9daf1",
"Id": "98598957df2641b1ab77685fc52efdc8"
},
{
"Start": "0ced997499ad442e8068622bcb567bc1",
"End": "73fd808a004d4e67b7d66ce8ad888a54",
"Id": "e64f9af715be4ec8b520e17eea1714ec"
},
{
"Start": "fa6580ad9a5b4ae4b33dd5cd5422b8e6",
"End": "496ce45912ae4178a10d2b1eeeede378",
"Id": "1db00d2bcf4e45d4a69eaa7697fb2b86"
},
{
"Start": "d6911c819ee942618c930b7d0b6e0d78",
"End": "dfd6fb927b2446c0bf2014259e3bc7be",
"Id": "88a66942c48449518a99f82484fb27da"
}
],
"Dependencies": [],
"NodeLibraryDependencies": [],
"Bindings": [],
"View": {
"Dynamo": {
"ScaleFactor": 1.0,
"HasRunWithoutCrash": true,
"IsVisibleInDynamoLibrary": true,
"Version": "2.10.0.3156",
"RunType": "Automatic",
"RunPeriod": "1000"
},
"Camera": {
"Name": "Background Preview",
"EyeX": -17.0,
"EyeY": 24.0,
"EyeZ": 50.0,
"LookX": 12.0,
"LookY": -13.0,
"LookZ": -58.0,
"UpX": 0.0,
"UpY": 1.0,
"UpZ": 0.0
},
"NodeViews": [
{
"ShowGeometry": true,
"Name": "List.SortByKey",
"Id": "aeff4f246990441aae2cea89cd5d2d5d",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 466.5,
"Y": 138.5
},
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "6a570493e8cc4b33ae1aec199b366dde",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 157.5,
"Y": -3.5
},
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "5490581cd07147699e9b895764274ef8",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 194.0,
"Y": 277.0
},
{
"ShowGeometry": true,
"Name": "Watch",
"Id": "ef7368e45fe4426d9c5827f250a1b040",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 804.0,
"Y": 263.0
},
{
"ShowGeometry": true,
"Name": "Watch",
"Id": "aaf179d971a54c30b603e41cee521dff",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Excluded": false,
"X": 861.0,
"Y": 55.0
}
],
"Annotations": [],
"X": 0.0,
"Y": 0.0,
"Zoom": 1.0
}
}