Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Array sort challenges

Community Expert ,
Nov 04, 2019 Nov 04, 2019

Edit: I resolved the bug. Unrelated to the sort. If you want to append text content, do it to the story and not a frame!

 

I have an Array of objects, each of which has the properties "first", "last" and "number". 

I am attempting to sort this array by extending array sorting, and need to be able to do it by either first, last or number. The numbers are saved as strings upon object instantiation. Below is the custom sort function I am trying, but it is not behaving well. If I sort by "last", then output the three properties by line, I am showing it working OK through about the B's, then it switches to reverse order, starting with Z's down to C's. If I sort by number, then I get 1-8 correct, then it lists 20, then 19, 18, etc. down to 9.  Any thoughts on how to fix/improve would be appreciated. I have verified that the array is being created appropriately and outputs correctly before I do the sort. 

 

 

 

 

function Obj(last, first, number) {
    this.last = last;
    this.first = first;
    this.table = number;
};

var sort = function (arr, sortOption) {
    arr.sort(function (a, b) {
        if (sortOption == "number") {
            a[sortOption] = parseInt(a[sortOption], 10);
            b[sortOption] = parseInt(b[sortOption], 10);
        }
        if (a[sortOption] < b[sortOption]) {
            return -1;
        } else if (a[sortOption] > b[sortOption]) {
            return 1;
        } else {
            return 0;
        }
    });
  };

sort(arr, "last");
    for (var i = 0; i < arr.length; i++) {
        textFrame.contents += arr[i].last + ", " + arr[i].first + "\t" + arr[i].number + "\r";
    }

 

 

 

What's bizarre with the output on last name sort is that when it switches from B to Z, the new Z last name with number appears in the place of the number on the same line as the B last name, with the B name's number appended to the end of the text file: 

 

 

ALast, AFirst	14
BLast, Bfirst	ZLast, ZFirst	17
YLast, Yfirst	15
...
BlastNumber

 

 

 If I do the output before running the sort, then do the sort, then do the output again, that section displays somewhat properly, though still out of order, like this: 

 

 

ALast, AFirst	14
BLast, Bfirst	15
ZLast, ZFirst	17
YLast, Yfirst	15
...

 

 

I'm mystified. 

TOPICS
Scripting
593
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 06, 2019 Nov 06, 2019

Works fine over here with a small array which I think has the correct format. It would help if you post your test array. Also, your function Obj() isn't used, so it's misleading in your script.

If your array is long and doesn't fit the text frame, you should use 

textFrame.parentStory.contents += . . .

rather than textFrame.contents += . . . . Could well be that that's the problem.

P.

Translate
Community Expert ,
Nov 06, 2019 Nov 06, 2019

Works fine over here with a small array which I think has the correct format. It would help if you post your test array. Also, your function Obj() isn't used, so it's misleading in your script.

If your array is long and doesn't fit the text frame, you should use 

textFrame.parentStory.contents += . . .

rather than textFrame.contents += . . . . Could well be that that's the problem.

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2019 Nov 06, 2019
LATEST

Thanks for the reply, Peter. Not appending to parentStory was the issue. I was going to delete this post but couldn't figure out how to in the new forum order. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines