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 8036831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:44:58+00:00 2026-06-05T02:44:58+00:00

I have an array of (always 4) objects, which I need to order by

  • 0

I have an array of (always 4) objects, which I need to order by descending value of an object member.
I had thought to order it as

Array = Array.OrderByDescending(p => p.Val)

This fell over when one of the values was null, of course. So what I am aiming for, but my LINQ is not up to, is:

Array = Array.OrderByDescending(p => if( p != null ) p.Val; else float.MinValue)

How can I accomplish this ordering without having to delete and later re-add the null value? Thanks for your help.

  • 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-05T02:45:00+00:00Added an answer on June 5, 2026 at 2:45 am

    Use the ternary conditional operator:

    Array = Array.OrderByDescending(p => p != null ? p.Val : float.MinValue)
    

    Per the comments below, the reason you can’t use the if/else is because the body of the lambda (the stuff to the right of p =>) must be an expression, unless you surround the whole thing with curly braces. So to illustrate, you could also use the if/else if you wanted:

    Array = Array.OrderByDescending(p => 
    {
        if (p != null) return p.Val; 
        else return float.MinValue;
    });
    

    But clearly more verbose.

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

Sidebar

Related Questions

I have an array which contains objects, these objects are in the same order
I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using
I have an array(sorted on an attribute called node_id) of ruby objects which basically
I have an Array nested withing an Object that's inside an Object which is
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array of strings, String[] data and it's 10 elements has value P
I have a complex map, which doesn't need locking for the frequent gets and
I have a JSON object with an array of updates, where each update consists
I have an object which has one field- double[] _myField it's hashcode is public

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.