Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com sorting two dimensional array with parseInt()

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-29-2012, 10:04 AM PM User | #1 rotezecke New Coder   Join Date: Sep 2010 Posts: 20 Thanks: 3 Thanked 1 Time in 1 Post sorting two dimensional array with parseInt() hi i have to sort a two dimensional array for google visualization api. the unsorted data works fine. structure of the unsorted array is: Code: [['stringA0',intB0], ['stringA1',intB1], ['stringA2'; intB2], ['stringA3'; intB3], ['stringA4'; intB4], ['stringA5'; intB5]] i have to sort on the second element. as demonstrated above, first element is a string, second an integer, and this should remain so. i use this function for sorting Code: myarray.sort(sortMultiDimensional); function sortMultiDimensional(a,b) { // this sorts the array using the second element return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0)); } but this turns the second element into a string. i didnt write the function (and i dont get it). where would i have to place parceInt() to make it work? cheers
 11-29-2012, 11:13 AM PM User | #2 rotezecke New Coder   Join Date: Sep 2010 Posts: 20 Thanks: 3 Thanked 1 Time in 1 Post i changed the function to Code: function sortMultiDimensional(a,b) { //make sure element stays an integer a[1] = parseInt(a[1]); b[1] = parseInt(b[1]); // this sorts the array using the second element return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0)); } and now things seem to be working.
 11-29-2012, 06:46 PM PM User | #3 007julien Regular Coder   Join Date: May 2012 Location: France Posts: 174 Thanks: 0 Thanked 27 Times in 25 Posts A return a[1]-b[1];in the function is enough to work with integers
 11-29-2012, 07:38 PM PM User | #4 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,097 Thanks: 0 Thanked 579 Times in 569 Posts What number base are the numbers in that you are converting to decimal using parseInt() - if they are already base 10 then you don't need to convert them and so don't need parseInt() To convert a string to a number you can 1. wrap it inside Number( ) 2. wrap it inside (+ ) 3. subtract something from it 4, multiply it by something 5. divide something into it As you need to do the subtraction anyway that will take care of the conversion. __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

 Bookmarks

 Tags arrays, javascript, multidimensional, parseint, sort

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:15 AM.