Sort complex array in swift
I have a array like this:
array = [[[2,2],[8,8]],[[4,4],[1,1]]]
I want to sort that array like this:
sortedArray = [[[1,1],[2,2]],[[4,4],[8,8]]]
Can anyone provide some kind of hint or solution for how to sort this array.
Thanks!
swift
add a comment |
I have a array like this:
array = [[[2,2],[8,8]],[[4,4],[1,1]]]
I want to sort that array like this:
sortedArray = [[[1,1],[2,2]],[[4,4],[8,8]]]
Can anyone provide some kind of hint or solution for how to sort this array.
Thanks!
swift
4
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
What if the array is[[[1,1],[9,9]],[4,4],[5,5]]]
?
– Rakesha Shastri
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
1
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35
add a comment |
I have a array like this:
array = [[[2,2],[8,8]],[[4,4],[1,1]]]
I want to sort that array like this:
sortedArray = [[[1,1],[2,2]],[[4,4],[8,8]]]
Can anyone provide some kind of hint or solution for how to sort this array.
Thanks!
swift
I have a array like this:
array = [[[2,2],[8,8]],[[4,4],[1,1]]]
I want to sort that array like this:
sortedArray = [[[1,1],[2,2]],[[4,4],[8,8]]]
Can anyone provide some kind of hint or solution for how to sort this array.
Thanks!
swift
swift
edited Nov 14 '18 at 12:30
fewlinesofcode
2,0671818
2,0671818
asked Nov 14 '18 at 12:27
Uday BabariyaUday Babariya
547621
547621
4
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
What if the array is[[[1,1],[9,9]],[4,4],[5,5]]]
?
– Rakesha Shastri
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
1
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35
add a comment |
4
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
What if the array is[[[1,1],[9,9]],[4,4],[5,5]]]
?
– Rakesha Shastri
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
1
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35
4
4
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
What if the array is
[[[1,1],[9,9]],[4,4],[5,5]]]
?– Rakesha Shastri
Nov 14 '18 at 12:33
What if the array is
[[[1,1],[9,9]],[4,4],[5,5]]]
?– Rakesha Shastri
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
1
1
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35
add a comment |
1 Answer
1
active
oldest
votes
Let's start with this array:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
Flatten it, and sort it by the first element of each sub array:
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
Now we'll need to group each element in an even index with the following element:
let start = flat.startIndex
let end = flat.endIndex
let even = stride(from: start, to: end, by: 2).map { flat[$0] }
let odd = stride(from: flat.index(start, offsetBy: 1), to: end, by: 2).map { flat[$0] }
let zipped = zip(even, odd)
let newArray = zipped.map { [$0.0, $0.1]}
And now you can use the result:
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
A more efficient way of creating pairs from elements at even and odd indices is to use this beautiful extension by matt:
extension Sequence {
func clump(by clumpsize:Int) -> [[Element]] {
let slices : [[Element]] = self.reduce(into:) {
memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clumpsize {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
return slices
}
}
And use it like so:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
let newArray = flat.clump(by: 2)
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300245%2fsort-complex-array-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's start with this array:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
Flatten it, and sort it by the first element of each sub array:
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
Now we'll need to group each element in an even index with the following element:
let start = flat.startIndex
let end = flat.endIndex
let even = stride(from: start, to: end, by: 2).map { flat[$0] }
let odd = stride(from: flat.index(start, offsetBy: 1), to: end, by: 2).map { flat[$0] }
let zipped = zip(even, odd)
let newArray = zipped.map { [$0.0, $0.1]}
And now you can use the result:
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
A more efficient way of creating pairs from elements at even and odd indices is to use this beautiful extension by matt:
extension Sequence {
func clump(by clumpsize:Int) -> [[Element]] {
let slices : [[Element]] = self.reduce(into:) {
memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clumpsize {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
return slices
}
}
And use it like so:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
let newArray = flat.clump(by: 2)
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
add a comment |
Let's start with this array:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
Flatten it, and sort it by the first element of each sub array:
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
Now we'll need to group each element in an even index with the following element:
let start = flat.startIndex
let end = flat.endIndex
let even = stride(from: start, to: end, by: 2).map { flat[$0] }
let odd = stride(from: flat.index(start, offsetBy: 1), to: end, by: 2).map { flat[$0] }
let zipped = zip(even, odd)
let newArray = zipped.map { [$0.0, $0.1]}
And now you can use the result:
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
A more efficient way of creating pairs from elements at even and odd indices is to use this beautiful extension by matt:
extension Sequence {
func clump(by clumpsize:Int) -> [[Element]] {
let slices : [[Element]] = self.reduce(into:) {
memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clumpsize {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
return slices
}
}
And use it like so:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
let newArray = flat.clump(by: 2)
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
add a comment |
Let's start with this array:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
Flatten it, and sort it by the first element of each sub array:
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
Now we'll need to group each element in an even index with the following element:
let start = flat.startIndex
let end = flat.endIndex
let even = stride(from: start, to: end, by: 2).map { flat[$0] }
let odd = stride(from: flat.index(start, offsetBy: 1), to: end, by: 2).map { flat[$0] }
let zipped = zip(even, odd)
let newArray = zipped.map { [$0.0, $0.1]}
And now you can use the result:
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
A more efficient way of creating pairs from elements at even and odd indices is to use this beautiful extension by matt:
extension Sequence {
func clump(by clumpsize:Int) -> [[Element]] {
let slices : [[Element]] = self.reduce(into:) {
memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clumpsize {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
return slices
}
}
And use it like so:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
let newArray = flat.clump(by: 2)
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
Let's start with this array:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
Flatten it, and sort it by the first element of each sub array:
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
Now we'll need to group each element in an even index with the following element:
let start = flat.startIndex
let end = flat.endIndex
let even = stride(from: start, to: end, by: 2).map { flat[$0] }
let odd = stride(from: flat.index(start, offsetBy: 1), to: end, by: 2).map { flat[$0] }
let zipped = zip(even, odd)
let newArray = zipped.map { [$0.0, $0.1]}
And now you can use the result:
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
A more efficient way of creating pairs from elements at even and odd indices is to use this beautiful extension by matt:
extension Sequence {
func clump(by clumpsize:Int) -> [[Element]] {
let slices : [[Element]] = self.reduce(into:) {
memo, cur in
if memo.count == 0 {
return memo.append([cur])
}
if memo.last!.count < clumpsize {
memo.append(memo.removeLast() + [cur])
} else {
memo.append([cur])
}
}
return slices
}
}
And use it like so:
let array = [[[2,2],[8,8]],[[4,4],[1,1]]]
let flat = array.flatMap { $0 }
.sorted { $0.first ?? 0 < $1.first ?? 0 }
let newArray = flat.clump(by: 2)
print(newArray) //[[[1, 1], [2, 2]], [[4, 4], [8, 8]]]
edited Nov 14 '18 at 14:21
answered Nov 14 '18 at 12:49
Carpsen90Carpsen90
7,65662760
7,65662760
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
add a comment |
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
perfect Answer..
– Uday Babariya
Nov 15 '18 at 11:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300245%2fsort-complex-array-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Are all elements in the sub-arrays equal? Are all powers of 2? Are they sorted? Do the sub-arrays have the same length?
– Cristik
Nov 14 '18 at 12:31
What if the array is
[[[1,1],[9,9]],[4,4],[5,5]]]
?– Rakesha Shastri
Nov 14 '18 at 12:33
Is it guaranteed that each nested array contains pair of equaled elements?
– Ahmad F
Nov 14 '18 at 12:33
1
You have like three-level nested arrays for what exactly? Could you enlighten a little more your problem?
– Guilherme Matuella
Nov 14 '18 at 12:35
You're going to have to move values out of these arrays into other array to... since 2 and 8 are within 1 array and 4 and 1 are in a different one so a sort based on the actual array is just going to throw back exactly what you already have as. As I suspect others will soon suggest, is there a reason for this amount of nesting, what are you actually trying to solve/store
– AngryDuck
Nov 14 '18 at 12:35