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})









share|improve this question















marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

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 a String, and it doesn't have a property videosValues.
    – 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















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})









share|improve this question















marked as duplicate by Tamás Sengel, rmaddy, user6655984, vacawama swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

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 a String, and it doesn't have a property videosValues.
    – 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













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})









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

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 swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

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 a String, and it doesn't have a property videosValues.
    – 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






  • 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












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].






share|improve this answer




























    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].






    share|improve this answer

























      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].






      share|improve this answer























        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].






        share|improve this answer












        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].







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 23:10









        bscothern

        18414




        18414















            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents