1

I read in a list that has 28 elements that look like this: 28.6 ft2. The list is read in not in numerical order, a bunch for different area floats. I then click on a filter on the page and it sorts the elements from least to greatest. I have an array thats preloaded with the data designed so when I do my first expect it matches exactly. I need to take the preloaded floatarray and sort them from least to greatest. Here is what I have for code:

var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2', '5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2', '6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2',
  '2,793.6 ft2', '1,195.0 ft2', '148.5 ft2', '39,077.1 ft2', '5,007.7 ft2', '1,746.4 ft2', '1,247.7 ft2', '230.0 ft2', '345.8 ft2', '114.1 ft2', '229.8 ft2', '349.3 ft2', '116.3 ft2',
  '235.2 ft2', '119.5 ft2', '3,038.3 ft2'
];
//the area element list
var area = element.all(by.css("[data-bind='html: area']"));

 //this has been shortened per request
  }).then(function() {
    browser.driver.get('http://iplan-qa.meetingmatrix.com/Apps/CapacityChart/mmidemo/auto/auto');
    browser.driver.sleep(2000);
    //clicks the area filter button
    element.all(by.css("[data-bind='text: displayName, visible: displayName']")).get(3).click().click();
    browser.driver.sleep(3000);
    area.count().then(function(count) {
      console.log(count);
      j = 0;

      function int_arr(a, b) {
        return parseFloat(a) - parseFloat(b);
      }
      areaArray = areaArray.sort(int_arr);
      areaArray = areaArray.reverse();
      for (var i = 0; i < count; i++) {
        //scrolls down the list element by element
        browser.executeScript("arguments[0].scrollIntoView();", area.get(i).getWebElement());
        area.get(i).getText().then(function(text) {
          console.log(text, areaArray[j], j);
          expect(text).toEqual(areaArray[j++]);
        });
      }

    });
  });
};

The results look like this:

5,089.8 ft2 5,089.8 ft2 0
2,511.5 ft2 2,511.5 ft2 1
15,076.8 ft2 15,076.8 ft2 2
5,019.8 ft2 5,019.8 ft2 3
471.4 ft2 471.4 ft2 4
462.9 ft2 462.9 ft2 5
5,149.3 ft2 5,149.3 ft2 6
1,753.6 ft2 1,753.6 ft2 7
6,520.5 ft2 6,520.5 ft2 8
//this is be shortened per request

39,077.1 ft2 1,247.7 ft2 0
15,076.8 ft2 1,746.4 ft2 1
6,520.5 ft2 1,753.6 ft2 2
5,149.3 ft2 1,195.0 ft2 3
5,089.8 ft2 2,511.5 ft2 4
5,019.8 ft2 2,678.5 ft2 5
5,007.7 ft2 2,793.6 ft2 6
3,327.4 ft2 3,038.3 ft2 7
3,192.9 ft2 3,327.4 ft2 8

5
  • 1
    What question are you asking? What is significane of the ` ft2` portion of the string? Do you have to use that string input as opposed, to say, an actual array of floats? Have you considered just going through a mapping the array of string to an array of floats and using the resuiltant array of floats for sorting using typical array sort methods? Commented Aug 25, 2015 at 14:28
  • So you want to sort the elements in areaArray from smallest to biggest? Commented Aug 25, 2015 at 14:29
  • @Mike it shows on screen as feet squared map cannot work because I cannot see the elements to read in that are off the screen unless I read line by line. Maps have been tried in a previous question I had asked. Commented Aug 25, 2015 at 14:47
  • @Tobias yes that is what i'm trying to do Commented Aug 25, 2015 at 14:48
  • @charlie if you look at the first paragraph it states what I'm trying to do. I will delete what I have I was just attempting to show the entire function to help. Commented Aug 25, 2015 at 14:49

2 Answers 2

3

You cant sort these data parsing the entire value to a number with this :

function int_arr(a, b) {
    return parseFloat(a) - parseFloat(b); // parseFloat('5,089.8 ft2') -> 5
}

You need to split the value on the space and convert the number to a float representation, removing the ,

so '5,089.8 ft2' become 5089.8 when used for comparison, and not 5

function int_arr(a, b) {
  var na = a.split(' ')[0].replace(',', '');
  var nb = b.split(' ')[0].replace(',', '');
  return parseFloat(na) - parseFloat(nb);
}

var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2',
 '5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2', 
 '6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2'
];

areaArray.sort(int_arr);


document.getElementById("result").innerHTML = JSON.stringify(areaArray);
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

6 Comments

When I do the expect it is looking for the ft2 as well, will this add ft2 back after?
this function is not updating the content of the original array. For the case of an Array of string a and b hold a copy of that string, so you can do whatever you want with these values
It sorts but in random spots. I'm assume what I need to do is remove ft2 and then essentially add it back to the array?
what do you mean by 'in random spots' ? I edited my post to show an example
What I see from your example answered my random spots question. Its sorting before the coma hence the random spots.
|
1

I personally would just convert the existing array to an array of floats and work with that array of floats instead. The commas and ft2 parts of those strings are simply display concerns and can be added when writing HTML to DOM.

var areaArray = ['5,089.8 ft2', '2,511.5 ft2', '15,076.8 ft2', '5,019.8 ft2', '471.4 ft2', '462.9 ft2', '5,149.3 ft2', '1,753.6 ft2', '6,520.5 ft2', '3,327.4 ft2', '3,192.9 ft2', '2,678.5 ft2',
  '2,793.6 ft2', '1,195.0 ft2', '148.5 ft2', '39,077.1 ft2', '5,007.7 ft2', '1,746.4 ft2', '1,247.7 ft2', '230.0 ft2', '345.8 ft2', '114.1 ft2', '229.8 ft2', '349.3 ft2', '116.3 ft2',
  '235.2 ft2', '119.5 ft2', '3,038.3 ft2'
];
var areaFloatArray = areaArray.map(function(item) {
    return parseFloat(item.replace(' ft2', '').replace(',', ''));
});

// then just work with areaFloatArray, which now contains float values
// you can later add back in commas and "ft2" for display

2 Comments

the dom and html are not my code I cannot change it, or I would. Oh how I would change so much. I am the sole automation person and they just threw me into it.
Also map functions could not work in my situation as in order for the elements to be read in I needed to scroll down line by line. This is why I posted the entire function so could see what exactly the function was doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.