Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:56:47+00:00 2026-06-04T13:56:47+00:00

I have various styles of font objects grouped into arrays. Each font object has

  • 0

I have various styles of font objects grouped into arrays. Each font object has a property sub_family which is either Regular, Italic, Bold, Bold Italic (or some variation e.g. Italic is sometimes called Oblique). When they come out of the database they are in a random order and need to be sorted as above; Regular is always first, Italic second, etc.

Here’s the sort function I’m using, it works but I wanted to see if you guys could come up with better/alternate solutions:

// input: ["Bold Italic", "Regular", "Bold", "Italic"]    
sortFont = function(a, b) {
    // regular
    if (/^([Rr]egular|[Nn]ormal)$/.test(a.sub_family)) { return -1; }
    if (/^([Rr]egular|[Nn]ormal)$/.test(b.sub_family)) { return 1; }
    // italic
    if (/^([Ii]talic|[Ii]nclined|[Ii]t|[Oo]blique|[Oo]bl)$/.test(a.sub_family)) { return -1; }
    if (/^([Ii]talic|[Ii]nclined|[Ii]t|[Oo]blique|[Oo]bl)$/.test(b.sub_family)) { return 1; }
    // bold
    if (/^([Bb]old|[Bb]d)$/.test(a.sub_family)) { return -1; }
    if (/^([Bb]old|[Bb]d)$/.test(b.sub_family)) { return 1; }
}
// output: ["Regular", "Italic", "Bold", "Bold Italic"]

thanks! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:56:48+00:00Added an answer on June 4, 2026 at 1:56 pm

    Here’s a massively more efficient way of doing it that also lets you maintain it just by adding entries to a table:

    sortKeys = {
        "regular": 1,
        "normal": 2,
        "italic": 10,
        "inclined": 11,
        "oblique": 12,
        "bold": 20,
        "bold italic": 30
    };
    
    function sortFont(a, b) {
        var aKey = sortKeys[a.sub_family.toLowerCase()] || 100;
        var bKey = sortKeys[b.sub_family.toLowerCase()] || 100;
        return(aKey - bKey);
    }
    

    Demo here: http://jsfiddle.net/jfriend00/MB76R/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string field dob which has birthdates in various formats e.g. dob
I have various time-series I'd like to correlate and present as either a csv-file
I have various pieces of generated HTML/JS that I load into Activity with WebView#loadDataWithBaseURL.
We have various related session variables for a complex page that has various things
I have various models of which I would like to keep track and collect
I have an application that declares textboxes in various places, like in styles and
I have a Silverlight app that has a bunch of styles that are referenced
In my application I have various styles and templates that are used in several
I am implementing a border control in win32, and I have implemented various styles
I have 2 questions Q1: I have read about various coding indent styles like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.