JS 陣列排序範例
馬老師離開待了約十幾年的教學界,目前在外商科技公司擔任Senior Consultant的工作,原因當然很多,未來有空再慢慢發文章分享,剛好最近有點時間,怕以後忘記,把最近專案中用到的一些程式筆記下來,如果大家有需要,也可以參考使用,這一篇是關於Javascript陣列排序的部分。
通常若有較多的內容需要儲存,變數就沒有陣列來的好用,所以陣列是拿來儲存大量的資料時所使用的,且儲存在裡面的資料,還可以選擇經過排序之後再呈現至畫面上,例如:
var name = ["stanley", "jack", "anita" , "mary"]; name.sort() //依照字母排序 console.log(name); // 輸出 ["anita", "jack", "mary", "stanley"] names.reverse() //反轉陣列內容 console.log(name); //輸出 ["stanley", "mary", "jack", "anita"]
但若我們同時有多個陣列,但希望以其中之一的內容排序時,也可以同步更新到另外一個陣列,該如何處理呢?可以參考以下的方式:
var name = ["stanley", "jack", "anita" , "mary"]; var gender = ["male" , "male" , "female" , "female"]; var score = [30, 10, 40 , 80]; var ID = ["S1" , "S2" , "S3" , "S4"]; console.log("name : " + name + "; score : " + score + "; gender : " + gender + "; ID : " + ID); /* 排序前 name : stanley,jack,anita,mary; score : 30,10,40,80; gender : male,male,female,female; ID : S1,S2,S3,S4; */ var list = []; for (var i = 0; i < name.length; i++){ list.push({ 'name': name[i], 'score': score[i], 'gender': gender[i], 'ID': ID[i] }); } list.sort(function(a, b) { return ((a.name < b.name) ? -1 : ((a.name == b.name) ? 0 : 1)); }); for (var i = 0; i < list.length; i++) { name[i] = list[i].name; score[i] = list[i].score; gender[i] = list[i].gender; ID[i] = list[i].ID; } console.log("name : " + name + "; score : " + score + "; gender : " + gender + "; ID : " + ID); /* 排序後 name : anita,jack,mary,stanley; score : 40,10,80,30; gender : female,male,female,male; ID : S3,S2,S4,S1; */
若是希望按照分數排序,則可以將sort function 修改為下:
//score 由小到大 list.sort(function(a, b) { return a.score - b.score }); //score 由大到小 list.sort(function(a, b) { return b.score - a.score });
補充:
上述的排序內容均以英文和數字為主,若是遇到中文可使用localeCompare進行,而排序的方式是漢語拼音順序,以下為範例:
var arr = ["二","五","四","一","三"]; //漢語拼音:一[yi], 二[er], 三[san], 四[si], 五[wu] console.log("排序前:" + arr); // 排序前:二,五,四,一,三 arr.sort(function(a,b){ return a.localeCompare(b, 'zh'); //排序後:二,三,四,五,一 }); console.log("排序後:" + arr); var arr = ["中文","英語","法國話", "京片子", "中國"]; //中文[zhong wen], 英語[ying yu], 法國話[fa guo hua], 京片子[jing pian zi], 中國[zhong guo] console.log("排序前:" + arr); //排序前:中文,英語,法國話,京片子,中國 arr.sort(function(a,b){ return a.localeCompare(b, 'zh'); }); console.log("排序後:" + arr); //排序後:法國話,京片子,英語,中國,中文 var arr = ["中文","英语","法国话", "京片子", "中国"]; console.log("排序前:" + arr); //排序前:中文,英语,法国话,京片子,中国 arr.sort(function(a,b){ return a.localeCompare(b, 'zh'); }); console.log("排序後:" + arr); //排序後:法国话,京片子,英语,中国,中文
Buy Verified Shopify Accounts
$250.00 – $450.00
Buy Verified Shopifi Accounts. Shopify premium accounts that are verified. Our accounts are designed to enhance your business through seamless integration. Start your journey to entrepreneurship today with complete confidence! Quality assured.
Our Service
✮ 100% US/UK Verified Tiktok Shop Account
✮ Number Verified & Access
✮ Email Verified and Access ID/Passport/Driving Licence Verified
✮ US SSN Verified Full
✮ EIN Verified
✮ Check your business details
✮ Shop Verified
✮ Tiktok business account verified
✮ Verification of Documents
✮ Address Verification
✮ Order Now & Receive Your Products Immediately
✮ Replacement Guarantee
✮ Customer Support Available 24/7
✮ Money Back Guarantee
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Tiktok Shop Accounts
$450.00 – $600.00
Buy Tiktok Shop Accounts. Want to increase your online presence? We sell TikTok Shop Accounts! We offer ready-made and verified accounts to increase visibility. Profit from TikTok’s massive audience to grow your business today.
✮ 100% US/UK Verified Tiktok Shop Account
✮ Number Verified & Access
✮ Email Verified and Access ID/Passport/Driving Licence Verified
✮ US SSN Verified Full
✮ EIN Verified
✮ Check your business details
✮ Shop Verified
✮ Tiktok business account verified
✮ Verification of Documents
✮ Address Verification
✮ Order Now & Receive Your Products Immediately
✮ Replacement Guarantee
✮ Customer Support Available 24/7
✮ Money Back Guarantee
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Google 5 Star Reviews
$25.00 – $200.00
Buy Google 5 Star Reviews. Google reviews with 5 stars can boost your credibility. We offer genuine reviews to help you improve your reputation online, increase sales, and attract new customers. Watch your business grow as you stand out from the competition.
Our Service..
✮ Fully Completed Profiles
✮ Guaranteed 100% Refund
✮ Google Reviews are permanent.
✮ 100% Satisfaction Guarantee
✮ The maximum time for Google reviews is 24-48 hours
✮ Accounts that are 100% stable and safe
✮ Quality Work at Real Value
✮ USA Real Non Drop Reviews
24/7 Hours Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Bybit Accounts
$150.00 – $200.00
Buy Verified Bybit Accounts. Find reliable, secure and verified Bybit Accounts for sale. Enjoy seamless trading while maintaining compliance with the platform’s regulations. Discover our services that will enhance your crypto journey. Unlock your trading potential by joining us today.
The Key Takeaways
◊ Servicebuyusa.com offers secure Bitbank accounts that are verified.
◊ Bitbank, a cryptocurrency exchange in USA/UK/CA is licensed.
◊ Verified Bitbank Accounts provide a smooth and compliant trading environment.
◊ We offer excellent customer service through our platform.
◊ The best choice of traders is to buy verified Bitbank Accounts.
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified OPay Accounts
$190.00 – $350.00
Buy Verified OPay Accounts. We offer verified OPay account to enable seamless payments. Secure and reliable services that are tailored to meet your specific needs will ensure a smooth experience managing your online finances. Discover your options now!
Our Service
⇒ Login information for OPay accounts
⇒ You can access the linked email account
⇒ Verified 100% with real documents
⇒ Accounts that are 100% ready for transactions
⇒ Guaranteed replacement for 60 days and money back
⇒ Fastest delivery is available
⇒ 24/7 Customer Service
24/7 Hours Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified KuCoin Accounts
$120.00 – $150.00
Buy verified kucoin accounts. Find out how you can buy verified KuCoin Accounts from us in confidence and with ease. Secure transactions, reliable service, and improved trading experience. Start your trading journey today by joining a vibrant community of crypto traders.
Our Service..
✮ 100% Safe & Secure account
✮ Email Verified Access and Email Authenticated Access
✮ Phone Verified and Access
✮ Selfie Verified
✮ Passport/ID Verified
✮ Verified by Address
✮ Replacement Guarantee
✮ 24 Hour Customer Service
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Zen Accounts
$350.00 – $550.00
Buy Verified Zen Accounts. Our verified Zen account for sale will allow you to enjoy a smooth gaming experience. You can enjoy online security and authenticity as well as immediate access. Buy a verified Zen account today and discover the infinite possibilities.
Our Service
✮ Accounts approved by 100%
✮ Verify your photo ID
✮ Email Verified
✮ The account has been verified and is fully active.
✮ Our customer service is available 24/7.
✮ instant delivery
✮ Full documents verified accounts.
✮ High-Quality Service
✮ Discounts on Account Price
✮ Old Accounts
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Apple Pay Accounts
$380.00 – $450.00
Buy Verified Apple Pay Accounts. We offer verified Apple Pay Accounts for seamless and safe payments. Digital transactions offer convenience and safety. Instantly access a payment solution that is tailored to your specific needs. Enjoy hassle-free online shopping!
Our Service..
✮ 100% USA/EU Verified Apple Account
✮ Email And Number Verified
✮ Validation of ID, passport
✮ The SSN is fully verified
✮ Green Dot Bank Verified
✮ Card Verified
✮ Replacement Guarantee
✮ Customer Support Available 24/7
✮ Money Back Guarantee
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified MoonPay Accounts
$250.00 – $320.00
Buy Verified MoonPay Accounts from us. Enjoy seamless transactions and reliable service as we offer secure, trusted accounts to elevate your cryptocurrency experience. Don’t miss out—get your verified account today!
Our Service
✮ 100% USA/EU Verified Moonpay
✮ Complete KYC verification
✮ Email & Number Verified
✮ Validation of ID, passport
✮ Address Verification
✮ Replacement Guarantee
✮ Transection history
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Coinbase Accounts
$170.00 – $250.00
Buy Verified Coinbase Accounts. Find out how to buy verified Coinbase Accounts with confidence. We offer reliable Coinbase accounts that ensure smooth trading and transactions. Our premium verified trading accounts will help you to improve your crypto-trading game.
Our Service..
✮ 100% USA/UK Verified Coinbase
✮ 100% KYC verification
✮ Email Verified
✮ Validation of ID, passport and driving license
✮ Transection Old Account
✮ Replacement Guarantee
✮ Instant Access and Treading
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Paxful Accounts
$120.00 – $200.00
Buy Verified Paxful Accounts. Want to improve your trading? We sell verified Paxful Accounts! You can enjoy seamless transactions, enhanced credibility and exclusive features. Our service provides top-notch customer support, ensuring a seamless account set up. Trade with confidence today.
Our Service..
✮ Full KYC Confirmed/Selfie Checked
✮ Email Login Access
✮ Confirmed with ID Card
✮ Confirmed with genuine telephone number and address
✮ Confirmed with Pre-loaded Card
✮ Full Confirmed Records
✮ Genuine IP confirmed, not an auto-created one
✮ Completely practical record confirmed
✮ 50+ past exchange history.
✮ USA, UK, CAN, AUS, KHM, COL & Othaer
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Binance Accounts
$250.00 – $350.00
Discover a reliable source to buy verified Binance accounts with ease and security. Optimize your trading experience and gain access to premium features by obtaining legitimate accounts from us, ensuring peace of mind and seamless transactions. Start trading today!
Our Binance Account Service….
*** Fully verified Binance account in the US and EU.
*** Email Verified.
*** Validate your ID/Driver’s License/Passport
*** Address Verification.
*** The Transection history of the Old account is available.
*** Get Instant Access and Treading.
*** Instant Delivery.
*** Customer Support is available 24/7.
*** 60 Days Money Back Guarantee.
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Paysera Accounts
$250.00 – $400.00
Buy Verified Paysera Accounts. Genuine accounts are available to enhance financial management and streamline online transactions. We offer quality customer service when you buy Paysera Accounts.
Our Service
⇒ 100% Satisfaction and Recovery Guaranteed
⇒ Email Verified
⇒ Phone Verified
⇒ Full SSN provided
⇒ Date of Birth Provided
⇒ Bank Statement Copy
⇒ Photo ID provided
⇒ Fully Verified with Documents
⇒ 100% Money-Back Guarantee
⇒ Full-completed profiles
⇒ USA, UK, CAN, AUS, EU, and other countries Paysera
⇒ 24/7 Customer Support
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Bluebirds Accounts
$150.00 – $250.00
<strong>Buy Verified Bluebirds Accounts. We offer the ease of purchasing verified Bluebirds account. Connect with more people online and enhance your presence. Our service is dependable and guarantees authentic engagement, as well as increased visibility on the digital scene.
Our Service
⇒ Genuine Phone Number
⇒ Gmail Verified
⇒ Attached With Bank Card
⇒ Debit/ Credit Card Added And Confirmed
⇒ Ssn And Router Number Verified
⇒ 100% Genuine Account
⇒ 100% Customer Satisfaction
⇒ No Transaction Problem Required
⇒ Money Transfer Without Limit
⇒ Replacement Guaranteed
⇒ Fast And Short Time Delivery
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Monzo Accounts
$450.00 – $550.00
Discover our service that offers verified Monzo account purchases. You can enjoy seamless banking when you have legitimate accounts that are tailored to meet your specific needs. Our trusted platform offers you financial convenience and freedom today. Secure your verified Monzo account hassle-free!
Our Service..
✮ 100% US/UK Verified Account *
✮ Number Verified & Access *
✮ Email Verified & Access*
✮ ID/Passport Verified *
✮ SSN Verified *
✮ A Resident Proxy *
✮ VCC (Virtual Credit Card) with Address *
✮ Bank Account and Routing Number *
✮ Fast Delivery & Use Immediately *
✮ Replacement Guarantee
24/7 Hours Repley Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
1вин вход 1win827.ru .
Buy Verified Webmoney Accounts
$260.00 – $430.00
If you’re looking to enhance your online financial transactions, consider purchasing a verified Webmoney account from us. With our commitment to quality and customer satisfaction, you can focus on your business while we take care of your payment solutions. Secure your verified Webmoney account today and join the ranks of satisfied users who trust us for their digital transaction needs.
Our Webmoney Account Service…
=> Guaranteed 100% Satisfaction and Recovery
=> Email Login Access
=> Passport Verified
=> Card + Bank added
=> Provide Full SSN
=> Copy of Driving Licence
=> Provide Date of Birth
=> First delivery
=> Support for 24/7 customers
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Skrill Accounts
$269.00 – $390.00
Buying a verified Skrill account can be a strategic decision for anyone engaged in online transactions. With improved security, instant transfers, and dedicated support readily available, you can confidently navigate the digital financial landscape. Partner with us today and elevate your online transaction experience!
Our Skrill Account Service…
=> Guaranteed 100% Satisfaction and Recovery
=> Email Login Access
=> Passport Verified
=> Card + Bank added
=> Provide Full SSN
=> Copy of Driving Licence
=> Provide Date of Birth
=> First delivery
=> Support for 24/7 customers
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified TransferWise Accounts
$270.00 – $450.00
Buying a verified TransferWise account from us means you can bypass the lengthy registration process and start enjoying money-saving benefits immediately. We offer comprehensive support throughout your transaction journey, so you can count on us to assist you whenever needed.
Our TransferWise Account Service…
=> Guaranteed 100% Satisfaction and Recovery
=> Email Login Access
=> Passport Verified
=> Card + Bank added
=> Provide Full SSN
=> Copy of Driving Licence
=> Provide Date of Birth
=> First delivery
=> Support for 24/7 customers
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Buy Verified Paypal Accounts
Rated 1.50 out of 5 based on 2customer ratings(2 customer reviews)
$140.00 – $240.00
Are you looking to purchase verified PayPal accounts for your business? We offer secure and verified PayPal accounts to individuals as well as businesses. Our professional solutions will help you to enjoy hassle-free payments and increased credibility. Begin your journey towards seamless online payment today!
Our PayPal Account Service…..
PayPal email and password.
Verified phone: Verify any country’s Phone
Card Verified.
Bank Verified.
Passport Verified.
SSN Verified.
Email logins for PayPal.
Use Visa Card or credit card in PayPal.
Use of Date of birth.
Driver’s License and Passport.
Please provide your name, city, Social Security number, and other details.
Receive and send money with no limits.
Replacement for 30 days.
24/7 Contact
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
visit this site wallet surf
Buy Verified Cashapp Accounts
$120.00 – $520.00
We offer verified and secure accounts that are suitable for both personal and business use. You can enjoy hassle-free, secure transactions and enhanced privacy. Cash App is the perfect solution to your financial needs.
Our Cashapp Account Service…
=> Guaranteed 100% Satisfaction and Recovery
=> Email Login Access
=> Enable 100% BTC withdrawal
=> Card + Bank added
=> Provide Full SSN
=> Provide Date of Birth
=> First delivery
=> Support for 24/7 customers
24/7 Contact:-
Email: usasellservice@gmail.com
Skype: USASELLSERVICE
Telegram: @usasellservice
Continued wallet surf
best site surf tech
1win.kg http://www.1win827.ru .
view surf wallet extension
Good write-up. I definitely love this website. Keep writing!
https://mexicanpharminter.com/# mexican pharmacy online order
canadian king pharmacy
Cześć! Zapraszamy na oficjalną legalny stronę internetową Casino Vavada dla polskich graczy! Oferujemy bogaty wybór gier kasynowych od 44 uznanych twórców, takich jak Truelab, Pragmatic Play, IGT, Endorphina, Evolution Gaming, Playtech czy BGaming. Nasz program lojalnościowy obejmuje 5 poziomów członkostwa (od początkującego aż po platynowego), a w sekcji z grami na żywo możesz doświadczyć emocji dzięki profesjonalnym krupierom.
more wallet surf