Okay, so let me tell you about this thing I was messing around with the other day: fields or murray. Sounds kinda weird, right? Well, buckle up, because it was a bit of a journey.
It all started when I was trying to sort some data, right? I had this array of objects, and each object had a couple of properties, “fields” and “murray”. I needed to sort them based on some criteria, but I was running into all sorts of headaches. First off, the data was inconsistent. Sometimes “fields” was an array, sometimes it was a string. “Murray” was even more unpredictable – could be null, could be an object, could be a number… you name it.
So, first thing I did was try to clean up the data. I figured, if “fields” is supposed to be an array, let’s make sure it’s always an array. So, I wrote this little function:
function ensureArray(value) {
if (*(value)) {
return value;
if (value) {
return [value];
return [];
Pretty simple, right? If it’s already an array, great. If it’s something else, wrap it in an array. If it’s null or undefined, just return an empty array. I ran this function on every “fields” property in my data. Felt good, like I was making progress.
Next, I tackled “murray”. This was trickier. I decided I needed a strategy. What I ended up doing was checking the type of “murray”. If it was an object, I’d leave it alone. If it was null or undefined, I’d replace it with an empty object. If it was anything else (a number, a string, whatever), I’d create a new object with a property called “value” and set that to the original value of “murray”. It looked something like this:
function normalizeMurray(value) {
if (typeof value === 'object' && value !== null) {
return value;
if (value === null value === undefined) {
return {};
return { value: value };
After cleaning up the data, I started on the sorting logic. The main problem was that I needed to compare the values inside the “fields” array (after cleaning them up) and the values inside the “murray” object(after normalizing them), and sometimes one or the other could be missing or empty.
I decided to prioritize “fields”. If an object had a “fields” array with at least one element, I’d use the first element for sorting. If it didn’t, I’d fallback to “murray”. If neither had anything useful, I’d just treat them as equal.
So the comparison looked something like this:
function compare(a, b) {
const aFields = ensureArray(*);
const bFields = ensureArray(*);
const aMurray = normalizeMurray(*);
const bMurray = normalizeMurray(*);
if (* > 0 && * > 0) {
return aFields[0].localeCompare(bFields[0]); // Assuming the fields are strings
} else if (* > 0) {
return -1; // a has fields, b doesn't
} else if (* > 0) {
return 1; // b has fields, a doesn't
} else {
// Both don't have fields, compare murray
if (* !== undefined && * !== undefined) {
return String(*).localeCompare(String(*)); // Convert to string for comparison
} else if (* !== undefined) {
return -1; // a has murray, b doesn't
} else if (* !== undefined) {
return 1; // b has murray, a doesn't
} else {
return 0; // Both don't have anything, consider them equal
This function first normalizes the `fields` and `murray` values for both objects being compared. It then checks if both `a` and `b` have valid `fields` arrays. If they do, it compares the first elements of those arrays. If only one of them has a valid `fields` array, it prioritizes the one with the `fields`. If neither has a valid `fields` array, it proceeds to compare the `murray` values.
And then I just used this compare function with the `sort` method on the array.
It was a bit of a pain, but in the end, it worked. I got my data sorted the way I needed it. The whole thing taught me a good lesson about data cleaning and defensive programming. You never know what kind of mess you’re going to encounter, so it’s always a good idea to be prepared.
Anyway, that’s my story about fields or murray. Hope it was somewhat interesting!