I have an array of arrays that I need to sort, but I'm having trouble getting it figured out. My main array (mainArr) looks like this:
mainArr = ({code:"1", date:"1/2/2001", status:"Active"},
{code:"2", date:"6/2/2004", status:"Terminated"},
{code:"3", date:"2/2/2003", status:"Transferred"},
{code:"4", date:"9/2/2003", status:"Active"});
I need to sort the mainArr by the dates in the objects. The list should end up like this:
mainArr = ({code:"1", date:"1/2/2001", status:"Active"},
{code:"3", date:"2/2/2003", status:"Transferred"},
{code:"4", date:"9/2/2003", status:"Active"}.
{code:"2", date:"6/2/2004", status:"Terminated"});
In most cases, you can use the sortOn method of Array. For instance, if you wanted to sort by 'code':
mainArr.sortOn("code");
This will sort the array using the code field of each object to determine the order.
However, as you wish to sort by dates (in a string format), sorting will give incorrect results (as ordering alphabetically and in date order are not the same). You could add a new property to each object in the array to make sorting easier, eg:
{code:"1", date:"1/2/2001", status:"Active"}
Adding the date in reverse order (sortableDate), it would become:
{code:"1", date:"1/2/2001", status:"Active", sortableDate:"2001/2/1"}
and you can then order with:
mainArr.sortOn("sortableDate");
Related
Let's say I want to use the range function (inside a ForEach loop) in Azure Data Factory to create an array which consists of integers. These integers represent API pages related to some ID which was given to us as a parameter in the ForEach loop.
I would use it like #range(1, int(varMaxApiPages)).
This gives me what I expect; an array of integers:
[1, 2, 3]
But would it be possible to append the related ID to these integers? So the result would be something like: [{"someID", 1},{"someID", 2},{"someID", 3}]?
Such as:
def appendToArray(varMaxApiPages):
arr1 = list(range(varMaxApiPages))
json_array = [];
for item in arr1:
jsonObejct = {"someID",item}
json_array.append(jsonObejct)
for item in json_array:
print(item)
appendToArray(3)
The correct json array should look like this [{"someID": 1},{"someID": 2},{"someID": 3}], we can achieve that. If you don’t want the colon, you can think of a way to replace it.
My debug result is as follows:
I declared 3 array type variables. Variable res is used to review the debug result.
In Set variable1 activity, assign the value to it via #range(1,3).
Then Foreach the arr1.
Inside Foreach activity, we can use Append variable activity, add expression #json(concat('{"someID":',item(),'}')). It will convert json string to json Object and append to the array jsonArray.
Outside Foreach activity, assign the value of array jsonArray to array res to review the result, you can omit this step and use array jsonArray directly.
That's all.
I'm having a response which is an json array. Each element has it's meaning and I would be able to describe it.
My array:
["1525032694","300","true"]
I've found an example in the documentation that describes an array and each element which is the same:
https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-request-response-payloads-fields-reusing-field-descriptors
But I would like to know how I can describe each of it as:
current timestamp, seconds to next measurement, should perform fota
My current test:
webTestClient.post().uri("/api//measurement/${SampleData.deviceId}")
.syncBody(SampleData.sampleMeasurement())
.exchange()
.expectStatus()
.isOk
.expectBody()
.consumeWith(document("add-measurement", responseFields(
fieldWithPath("[]")
.description("An array of device settings"))
))
Ok, it turns that be fairly easy:
responseFields(
fieldWithPath("[0]").description("Current timestamp"),
fieldWithPath("[1]").description("Device mode"),
fieldWithPath("[2]").description("Device parameter")
),
I have two lists that look like this:
List("key1", "key2")
List("val1", "val2")
I'm trying to get it into this JSON-like format:
[{"key1":"val1"},{"key2","val2"}]
Right now, this is what I'm using:
val output = List(attrKeys.zip(attrValues).toMap)
But it gives me this:
[{"key1":"val1","key2":"val2"}]
How do I get this into a list of separate map objects?
attrKeys.zip(attrValues).map(Map(_))
How can I sort an object based on a property when that property contains special characters such as ä,ü,ö in angularjs using orderBy?
For example if I sort the object users based on the name property,
$scope.users = [
{name:'A', value:'1'},
{name:'B', value:'2'},
{name:'Ä', value:'3'},
{name:'Ü', value:'4'},
{name:'U', value:'5'}
];
it should return:
{name:'A', value:'1'},
{name:'Ä', value:'3'},
{name:'B', value:'2'},
{name:'U', value:'5'},
{name:'Ü', value:'4'}
Sort order is determined doing a lexicographical sort by comparing Unicode (z: U+005A comes before e: U+0065).
Have a look at this article which presents two different solutions to your problem.
Needed to sort Objects (Class/Datatype), Alphabetically.
UnSorted Array::
[main.Apple, main.Zoo, main.IceCream, main.Apple, main.IceCream]
Sorted Array:: Alphabetically Type # the Front
[main.Apple, main.Apple, main.IceCream, main.IceCream, main.Zoo]
This is one of the solutions that I have found till now:
Make use of sortInPlace function and dynamicType to check for class names e.g.:- String(firstDataType.dynamicType).componentsSeparatedByString(".").last! this retrieves the class name for the Object.
unsortedArray.sortInPlace({(firstDataType,secondDataType) in
return String(firstDataType.dynamicType).componentsSeparatedByString(".").last!
.localizedStandardCompare(String(secondDataType.dynamicType).componentsSeparatedByString(".").last!) == NSComparisonResult.OrderedAscending
})
Demo Link