How can I sort an array of Strings by another array of Ints in Swift 4 [duplicate]
up vote
-3
down vote
favorite
This question already has an answer here:
In Swift how can I sort one array based on another array?
3 answers
I'm using this code in the Playground as an example:
let videosUnsorted = ["C Video", "G Video", "L Video", "T Video", "S Video ", "P Video", "A Video", "Z Video", "R Video"]
let videosValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let sorted = videosUnsorted.sorted(by: {$0.videosValues > $1.videosValues})
print(sorted)
but it's giving me this error:
Argument passed to call that takes no arguments
I tried the following but it's not what I'm trying to accomplish. It's only sorting by the elements in the same array:
let sorted = videosUnsorted.sorted(by: {$0.1 < $1.2})
arrays swift
marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 0:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-3
down vote
favorite
This question already has an answer here:
In Swift how can I sort one array based on another array?
3 answers
I'm using this code in the Playground as an example:
let videosUnsorted = ["C Video", "G Video", "L Video", "T Video", "S Video ", "P Video", "A Video", "Z Video", "R Video"]
let videosValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let sorted = videosUnsorted.sorted(by: {$0.videosValues > $1.videosValues})
print(sorted)
but it's giving me this error:
Argument passed to call that takes no arguments
I tried the following but it's not what I'm trying to accomplish. It's only sorting by the elements in the same array:
let sorted = videosUnsorted.sorted(by: {$0.1 < $1.2})
arrays swift
marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 0:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
$0 is aString
, and it doesn't have a propertyvideosValues
.
– Carpsen90
Nov 14 at 22:58
1
See this question
– vacawama
Nov 14 at 22:59
1
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
This question already has an answer here:
In Swift how can I sort one array based on another array?
3 answers
I'm using this code in the Playground as an example:
let videosUnsorted = ["C Video", "G Video", "L Video", "T Video", "S Video ", "P Video", "A Video", "Z Video", "R Video"]
let videosValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let sorted = videosUnsorted.sorted(by: {$0.videosValues > $1.videosValues})
print(sorted)
but it's giving me this error:
Argument passed to call that takes no arguments
I tried the following but it's not what I'm trying to accomplish. It's only sorting by the elements in the same array:
let sorted = videosUnsorted.sorted(by: {$0.1 < $1.2})
arrays swift
This question already has an answer here:
In Swift how can I sort one array based on another array?
3 answers
I'm using this code in the Playground as an example:
let videosUnsorted = ["C Video", "G Video", "L Video", "T Video", "S Video ", "P Video", "A Video", "Z Video", "R Video"]
let videosValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let sorted = videosUnsorted.sorted(by: {$0.videosValues > $1.videosValues})
print(sorted)
but it's giving me this error:
Argument passed to call that takes no arguments
I tried the following but it's not what I'm trying to accomplish. It's only sorting by the elements in the same array:
let sorted = videosUnsorted.sorted(by: {$0.1 < $1.2})
This question already has an answer here:
In Swift how can I sort one array based on another array?
3 answers
arrays swift
arrays swift
edited Nov 14 at 23:03
rmaddy
236k27308374
236k27308374
asked Nov 14 at 22:53
Talib Kareem
11
11
marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 0:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 0:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
$0 is aString
, and it doesn't have a propertyvideosValues
.
– Carpsen90
Nov 14 at 22:58
1
See this question
– vacawama
Nov 14 at 22:59
1
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02
add a comment |
$0 is aString
, and it doesn't have a propertyvideosValues
.
– Carpsen90
Nov 14 at 22:58
1
See this question
– vacawama
Nov 14 at 22:59
1
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02
$0 is a
String
, and it doesn't have a property videosValues
.– Carpsen90
Nov 14 at 22:58
$0 is a
String
, and it doesn't have a property videosValues
.– Carpsen90
Nov 14 at 22:58
1
1
See this question
– vacawama
Nov 14 at 22:59
See this question
– vacawama
Nov 14 at 22:59
1
1
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
The easiest way is to use zip()
like this:
zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }
This combines the 2 arrays into the type [(String, Int)]
you then sort by the Int
values and use map()
to get back out just the sorted [String]
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The easiest way is to use zip()
like this:
zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }
This combines the 2 arrays into the type [(String, Int)]
you then sort by the Int
values and use map()
to get back out just the sorted [String]
.
add a comment |
up vote
1
down vote
The easiest way is to use zip()
like this:
zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }
This combines the 2 arrays into the type [(String, Int)]
you then sort by the Int
values and use map()
to get back out just the sorted [String]
.
add a comment |
up vote
1
down vote
up vote
1
down vote
The easiest way is to use zip()
like this:
zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }
This combines the 2 arrays into the type [(String, Int)]
you then sort by the Int
values and use map()
to get back out just the sorted [String]
.
The easiest way is to use zip()
like this:
zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }
This combines the 2 arrays into the type [(String, Int)]
you then sort by the Int
values and use map()
to get back out just the sorted [String]
.
answered Nov 14 at 23:10
bscothern
18414
18414
add a comment |
add a comment |
$0 is a
String
, and it doesn't have a propertyvideosValues
.– Carpsen90
Nov 14 at 22:58
1
See this question
– vacawama
Nov 14 at 22:59
1
What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want.
– rmaddy
Nov 14 at 23:02