RDowns
RDowns

Reputation: 659

Why does this array not reverse in the correct order?

I have an array that is outputting the following:

[L,W,L,W,L,L,W,W]

This is the correct values but I would like it reversed so it reads

[W,W,L,L,W,L,W,L]

This is my code:

for value in forms {

    if let arr = [value] as? [[String:Any]] {

        self.leagueForm = arr.flatMap { Form($0) }

        for form in self.leagueForm {
            self.formGuide.append(form.player_result!)
            self.formGuide = self.formGuide.reversed()

        }                       
    }
}
print ("update" ,self.formGuide)

Without the line self.formGuide = self.formGuide.reversed() it has the correct values but wrong order, with this line it changes the output completely to:

[W,L,W,W,L,L,L,W]

This is totally incorrect???

UPDATE: info as requested:

[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("93"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("90"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("89"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-23"), result_id: Optional("88"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("57"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("L"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("56"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("55"))]
[KeepScore.ShowCommunityViewController.Form(player_result: Optional("W"), player_name: Optional("Dicky"), result_date: Optional("2017-02-22"), result_id: Optional("54"))]`

Updated code:

if let forms = json?["playerForm"] as? [[String: Any]] {

    print (forms)

    for value in forms {

        if let arr = [value] as? [[String:Any]] {

            self.leagueForm = arr.flatMap { Form($0) }
            self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()

        }
    }
    print ("update" ,self.formGuide)

Upvotes: 1

Views: 119

Answers (4)

Nirav D
Nirav D

Reputation: 72410

The problem is that you are making array reversed inside the for loop where you are adding object in array that is the reason you are getting this issue.

To solve this simply put line reversedArray = reversedArray.reversed() after the for loop or avoid the for loop and use flatMap same way you have used to create Array of object from Array of Dictionary.

self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()

Edit: You are adding for loop inside the if let and there is no necessary of it.

if let forms = json?["playerForm"] as? [[String: Any]] {

    self.leagueForm = forms.flatMap { Form($0) }
    self.formGuide = self.leagueForm.flatMap { $0.player_result }.reversed()
}

Upvotes: 2

Yassir
Yassir

Reputation: 80

You can maybe keep your first code example and just put the self.formGuide = self.formGuide.reversed() after the for loop

for value in forms {
    if let arr = [value] as? [[String:Any]] {
        self.leagueForm = arr.flatMap { Form($0) }

        for form in self.leagueForm {
            self.formGuide.append(form.player_result!)
        }
    }
}

self.formGuide = self.formGuide.reversed()
print ("update" ,self.formGuide)

Upvotes: 1

zero3nna
zero3nna

Reputation: 2918

You could simply just call

self.formGuide.reverse()

where you want to access the array in reversed order, for example in your further code execution or to store it. Just dont call it in the loop where you adding stuff to the array.

Upvotes: 2

Bhanupriya
Bhanupriya

Reputation: 1202

I think you need

self.formGuide = forms.reversed()

Upvotes: 3

Related Questions