# Is my coding right?

**URL:** <https://forum.kirupa.com/t/is-my-coding-right/148063>\
**Category:** Uncategorized\
**Created:** [May 16, 2005, 9:08pm UTC](https://forum.kirupa.com/t/is-my-coding-right/148063 "2005-05-16T21:08:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Che](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Che](https://forum.kirupa.com/u/Che)\
**Post date:** [May 16, 2005, 9:08pm UTC](https://forum.kirupa.com/t/is-my-coding-right/148063/1 "2005-05-16T21:08:57Z")

</div>

I’m trying to sort a small array. The problem here is that the sort function is sorting the array it’s applied to PLUS another array that is just hanging out next to it. It’s weird. Anyone know what’s happening here?

```auto

function sortByNumber(a, b)
{
     return(a < b);
}

sort_array = new Array();
out_array = new Array();
cmp_name_array = new Array();
orig_array = new Array();
temp_array = new Array();
orig_array = [3, 5, 1, 2, 4];
trace("orig_array before sort: " add orig_array);
cmp_name_array = ["graph3", "graph5", "graph1", "graph2", "graph4"];

temp_array = orig_array;

sort_array = temp_array;
trace("sort_array before sort: " add sort_array);
trace("temp_array before sort: " add temp_array);
sort_array.sort(sortByNumber);

trace("cmp_name_array: " add cmp_name_array);
trace("orig_array after sort: " add orig_array);
trace("sort_array after_sort: " add sort_array);
trace("temp_array after sort: " add temp_array);

out_array = new Array();
trace("orig_array length: " add orig_array.length);

for(t=0; t< orig_array.length; t++)
{
     
     temp = orig_array[t];
     for(j=0; j< sort_array.length; j++)
     {
          
          if(temp == sort_array[j])
          {
               out_array[j] = cmp_name_array[t];
          }
     }
}

```

**Here’s the output for this:**

orig\_array before sort: 3,5,1,2,4

sort\_array before sort: 3,5,1,2,4

temp\_array before sort: 3,5,1,2,4

cmp\_name\_array: graph3,graph5,graph1,graph2,graph4

orig\_array after sort: 5,4,3,2,1 //this should NOT be sorted

sort\_array after\_sort: 5,4,3,2,1 //this SHOULD be sorted

temp\_array after sort: 5,4,3,2,1

orig\_array length: 5

out\_array: graph3,graph5,graph1,graph2,graph4

trace("out\_array: " add out\_array);
