AngularJS orderBy for multi-column sorting in different sort directions…

This is a fun issue to approach. I had a table of items which needed specific sorting on two fields (both descending) but the main sort needed to be dynamic based on one of five other fields with order (asc vs desc) toggling.

I’m using angularjs in this project. I had no trouble sorting by a single field and toggling the order understanding paystubs. I could also use orderBy to sort by multiple fields, but they all require the same ordering, learn about Lake Zurich Web Designers. There may be a trick to this that I couldn’t figure out, but the problem I was seeing is this:

I was using a $scope variable to designate the order direction called “reverse”. The column headings in my table had an ng-click event added to change another $scope variable called “orderByField” and would reverse the boolean of “reverse”.

 

This made the sorting dynamic for the column clicked, and if the column was clicked again, the order direction would switch as well. I’m not including the controller code because it’s just setting default values for the $scope.reverse and $scope.orderByField values.

This was fine until a change request was made to add additional sorting rules which aren’t easily accomplished with angular’s orderBy filter. The stipulation is that one field should always be ordered a certain way inside each set of orderByField groups.

Let me explain it with an example to better illustrate the issue. This is a simplified version of what I was dealing with:

items = [
  { start_time: '10', guaranteed: 1, name: 'test 1' },
  { start_time: '10', guaranteed: 0, name: 'test 2' },
  { start_time: '11', guaranteed: 1, name: 'test 3' },
  { start_time: '11', guaranteed: 0, name: 'test 4' }
];

The idea was if the column heading representing the start_time field is clicked that the items in the array would be sorted by start_time (either desc or asc). So start_time = 10 would be grouped together and start_time = 11 would be grouped together. Then within each of those groups, the ‘guaranteed’ field always needs to be sorted descending. So all guaranteed = 1 should be at the top of the start_time groups. There was actually a third field ordering stipulation within those groups but for simplification we’ll leave that one out.

My first thought was to use something like this:

 

It’s not recommended to use orderBy this way for good reason. It doesn’t work correctly. It worked fine in Firefox actually, but failed miserably in Chrome. The recommend method of sorting by multiple columns is to use something like the following:

 

The reason this wouldn’t work for me is because the “reverse” order direction switches not just the order direction of orderByField but also ‘guaranteed’. This wasn’t the desired result.

My next step was to create a custom orderBy filter which would do what I wanted. This example is more verbose in the if statements than it needs to be, but it’s easy to read this way:

app.filter("myOrderBy", function() {
    return function(items, field, reverse) {
        // sort by selected field first
        if(!reverse) {
            items.sort(function(a, b) {
                if(a[field]  >  b[field]) { return  1; }
                if(a[field] == b[field]) {
                    if(a['guaranteed']  <  b['guaranteed']) { return 1;  }
                    if(a['guaranteed'] == b['guaranteed']) { return 0;  }
                    if(a['guaranteed']  >  b['guaranteed']) { return -1; }
                }
                if(a[field]  <  b[field]) { return -1; }
            });
        } else {
            items.sort(function(a, b) {
                if(a[field]  <  b[field]) { return  1; }
                if(a[field] == b[field]) {
                    if(a['guaranteed']  <  b['guaranteed']) { return 1;  }
                    if(a['guaranteed'] == b['guaranteed']) { return 0;  }
                    if(a['guaranteed']  >  b['guaranteed']) { return -1; }
                }
                if(a[field]  >  b[field]) { return -1; }
            });
        }
        return items;
    }
});

Intermittent Fasting – My 8 Month Update

Last year, after a couple of bad years in my life, I started focusing more on my health. It actually started in August 2018 when I quit smoking. Quitting smoking caused me to gain about twenty pounds. I was already overweight. The extra weight was a necessary evil to quit smoking and improve my health.

I went from 224 pounds to 256 pounds within about 6 months. I would probably be over 270 right now if I hadn’t taken steps to curve this and correct it. I was walking daily to try to curve it, but that wasn’t doing the trick. I continued to gain weight because I couldn’t control my eating. I made a huge change to my diet in May of 2019.

Leading up to that time, I had developed many problems with my hemorrhoids treatment that I attributed to getting older and being a smoker. My body had food intolerances. I had dark spots on my skin around my neck, shoulders, chest, and underarms. My joints hurt. I just didn’t feel mobile.

All of these things made it difficult to be active. I walked to try to curve some of this, but it didn’t help much. I was still achy. My body just felt bad. I would get headaches if I had sugar before going to bed. I got hangry, and I mean HANGRY. My hunger affected my mood in extreme ways. I couldn’t eat bananas, avocados, or mushrooms without severe stomach pains for hours afterward.

So in May of 2019, I cut my carbs completely out. I did this for three weeks. I may have had 10 grams of carbs during that entire 21 days. The first three days were rough. I felt terrible. My body was going through sugar withdrawals. Suddenly after three days, I got a burst of energy and it stayed level. It wasn’t up and down. I just had a sustained boost to my energy level. There were supplements that I was taking at the time that could have had an effect on me, but for the most part, I feel like it was the change in my diet that was the biggest factor.

Keto vs Low-carb

It’s important at this point to understand the difference in “Keto” and “Low-carb”. Sometimes people claim they are on a Keto diet when they are actually just reducing their carbs. The distinction must be made because reducing your carbs does not guarantee that you are going to be in ketosis. For the first three weeks of my diet change, I was low-carb. I wasn’t concentrating on what I was eating. I was concentrating on what I wasn’t eating.

When most people do “low-carb” they replace their carb intake with protein. They eliminate sugar because it’s a carb, but by replacing their carbs with protein, they are only forcing their body to produce glucose in a different way, by converting the protein. So glucose levels can still be high with a high protein diet. If your glucose levels are high, your body won’t create ketones. Thus, you won’t go into ketosis.

I wasn’t on a “keto” diet. Keto involves a huge increase in fat intake. Your primary source of calories becomes fat. When I went low-carb, I simply removed carbs from my diet. Most of my calories came from meat and fats during that time. There were probably times that I went into ketosis, but the high protein diet I was doing probably prevented me from hitting a good level of ketosis. Looking back, I should have concentrated on consuming more fat.

During that first three weeks, I lost around 16 pounds. I went from 256 down to 240. I felt like I could do anything at that point. I celebrated by having bread for the first time in three weeks. Let me say, Sister Shuberts yeast rolls with butter after three weeks of nearly 0 carbs is about as close to heaven as I’ve been in a long time.

Low Carb Side-effects

After reading through everything I did on NatuRoids – I’ve been watching my carbs pretty closely for the last 8 months, and I’ve learned that there are a few side effects, at least for me. The only important one I’ll mention is digestion. I had to supplement my diet with laxatives quite a bit. There are gentle all-natural laxatives that worked well.

The best alternative is to choose your carbs more wisely. I tend to shoot for carbs which come along with a lot of fiber. Pinto beans, for instance, are loaded with carbs, but are also high in protein and fiber. Brown whole grain rice has a lot of carbs, but it is also high in fiber. Vegetables in general are good choices when sacrificing some carbs, because they tend to also help digestion quite a bit. Kale is especially good for my digestion and since it has quite a bit of potassium, it is something I try to get a lot of these days.

The Start of IF

Intermittent fasting was my go to lifestyle change strategy after the three weeks of low-carb. Based on all the propaganda I was reading at the time, I decided that IF was the right way to go. I’m still convinced that it’s the best way to maintain my weight, but I’ve found that some of the promises of IF are misleading or wrong when it comes to my body.

Fasting Increases Metabolism

This is something you’ll hear a lot from many experts. Dr. Jason Fung, who is famous for writing books about obesity and fasting, has stated that fasting increases metabolism. I’ve also seen other doctors and nutrionists report this. However, I am 95% sure that since I started IF, my metabolism has slowed way down. I have numbers and pseudo-science to back it up. Primarily my Fitbit gives me an estimate of my daily calorie burn, and that number decreased significantly when I started fasting daily.

I think that extended fasting probably increases one’s metabolism, if it’s done on rare occasions. However, I think that daily IF has been a significant factor in my metabolism slowing.

Perhaps a Slow Metabolism Isn’t Bad

I’m on the fence about whether a slow metabolism is a completely negative thing. Sure it’s much harder to lose weight when your metabolic rate is slower, but perhaps there’s other benefits that we are overlooking. Specifically, I wonder if a slower metabolism may be better for longevity.

I’ve tried to normalize/equalize my metabolism by increasing my activity level. I think the increase in activity/exercise is a positive change. My metabolism was actually extremely high last year. I could eat a lot of food in a given day, and my body was burning most of the calories during the day with no problems. It was doing it all at rest, sitting in my chair. My heart rate was around 93 bpm at rest.

Improvements Since Low-carb/Keto/IF

So I’ve been mixing it up for the last eight months, but I’ve pretty much consistently done low-carb and IF. I’ve also attempted pure Keto during that time, but I find Keto to be much harder to keep up, especially while increasing my daily activity level. I find straight IF to be the easiest to do along with lots of exercise.

I’ve seen some major changes in my body since I started all of this nearly a year ago.

Firstly, exercise is much easier. I can get around much better. I have way less joint pain and I feel way more mobile.

Secondly, all the dark spots on my skin have disappeared. I suspect they were from insulin resistance and they were a warning sign of future diabetes.

Thirdly, all of my food intolerance have pretty much been eliminated. I can eat bananas, avocados, and mushroom with relatively no issues. The only one of the three that I still occasionally have issues with is bananas. The other two are good to go.

Fourthly, I can think more clearly. My mind has gotten back some of its old pep. I can concentrate better. I can work more effectively.

Finally, my resting heart rate is now 58. I’m trying to get it even further down. I’m shooting for 45. Part of this significant change has been my increase in cardio exercise, but cardio alone wasn’t fixing the problem. The real change occurred when I cut out carbs for those first 3 weeks. During that time, my resting heart rate dropped from 93 down to 70. Over the next few months it continued to drop.

I now try to do OMAD (one meal a day) and I am experimenting with different levels of carbs. I’m also watching for improvements from specific foods. Kale has been major breakthrough. I’m not one to follow a lot of hype or fads, despite what it may seem. IF makes sense to me. Kale was not something I was interested in eating, but after I found some very good ways to incorporate it into my diet and after seeing the results it gives me, I now use every opportunity to have some. I’ve tried many “super foods” and kale is the only one that actually impressed me.

I have went from a 44 inch waist down to a 34 inch waist. I actually need to go shopping for new pants because I’m down to only one pair of jeans that actually fits me well. I want to get my waist down to 30. That is going to take another 20 pounds at least. However, I’ve trimmed up quite bit without losing a lot of weight. I take that to mean that I’m increasing my lean mass while losing fat. My overall body shape has changed for the better. I’m still a long ways from my goal body, but this is all going to have to happen in stages.

There’s one final bit of advice that I have for anyone who has lasted all the way through this post. You may have heard the saying “it’s a marathon not a sprint”, but when it comes to weight loss and body transformation it’s the only truth to remember. It takes a huge amount of time to get the results you are looking for usually. It’s really hard to go from overweight to ripped. I’m well on my way and I’m starting to see the results. I have felt the results for a long time, but visually I’m really starting to see the changes in my body. They are taking way longer than I wanted them to, but slow and steady wins the race.

Episode 8 is Terrible

Of course I’m going to go watch Star Wars Episode 9. Of course it’s going to make a huge amount of money. Of course some people are going to love it, and some people will hate it. It has had a ton of hype over the last few months. There have been plenty of spoilers/leaks released on Reddit.

As I gear up to go watch the last installment of the Skywalker saga, I have been watching all the previous installments in a marathon. I do this at least twice a year anyway, but it seemed like a prerequisite to watching the finale. I’m sitting here watching the last film, and I can’t believe how bad it was.

Did it shake things up? Sure, but was it necessary. Star Wars is pretty straight forward and doesn’t need all the crap Episode 8 has in it. Was it necessary to be this ruthless toward all the fans just to be artistic or to “take it in a new direction”. I’m not even sure what the direction or purpose of Episode 8 was other than to just slap all the old fans in the face.

Some people liked it. I’m still not sure why. As a stand-alone movie, it’s ridiculous. As a continuation of “The Force Awakens” it was just plain bad. The Last Jedi could have been so much better, but it seems like it was made poorly just to make political points or something. It feels like there’s a whole feminist agenda behind it, and no one cared if the movie was good or not. They just wanted to push the feminist agenda on the mostly male Star Wars fans.

There’s so many people who are apologetic of Episode 8. It doesn’t make any sense. The plot was terrible. Characters from decades ago were destroyed. Characters from Episode 7 were destroyed without purpose. Questions raised in Episode 7 were shot down without purpose. There’s side plots that completely fail and make no difference to the overall story, almost as if it’s just filler. It’s like they had no idea what they were going to do with the movie so half of it is wasted on this whole “master code-breaker” subplot. On that note, wow what an amazing name for someone. In a galaxy far far away, there’s a person named “The Master Code breaker”. I’m sure that’s not his real name, but why do they constantly refer to him as such.

That’s like calling Jabba the Hutt something like “The Big Bad Mob Boss”. Why don’t they just call the guy by his name? Sure he could have the fame of being the “master code breaker” but why do they have to keep referring to him as that?

This guy was supposed to be the only one that could do what they needed. Yet, they just happened to end up in a cell with a guy that could do the same thing. Then, he betrays them and makes the entire subplot useless.

The part that I hate the most has to be the selfless kamikaze death of the vice admiral. Before that we get to see her be secretive about a plan for no reason other than to build up to this point as well. We see how all of us should just believe her and trust her for no reason. Then as everyone is escaping, we see her left behind to fly the ship in a suicide mission.

The part that kills me about this is that for some reason the ship had to be flown manually. Really? In a reality that is this technologically advanced, they don’t have remote piloting? There’s no way to control the ship other than to be inside it? Not only that but all the sudden we can fly a ship at light speed into a whole bunch of enemy ships and take most of them out. Why haven’t we been doing this the whole time? Why hasn’t someone said, “You know, we should just remotely fly a space ship at hyperspace into the whole First Order / Empire fleet?” This could have saved a lot of trouble in previous movies.

There’s so much more wrong with this movie, but I will stop so that I can finish the thing and hope that Episode 9 will at least not suck as bad as it. I tried to like Episode 8. I actually loved Episode 7. I also liked the prequels. There were problems with them, but nothing in comparison to Episode 8. The Last Jedi is just terrible. I wish that it wasn’t, and I’m sorry to everyone who disagrees. It doesn’t work, and I can’t see a way that Episode 9 could make Episode 8 better. I hope it does, but I just don’t see it.

SEO Slug plugin breaks WordPress on PHP 7 Server

Well after a couple of years without posting on this blog, I decided to come back and start posting. Well I upgraded WordPress and then wrote a new post what you need to know about a paystub. Two hours later and I finally fixed a problem that kept giving a 500 error when trying to add the Post implementing payroll system.

Now I have to post how to fix the problem. I changed a lot of settings and finally narrowed it down to the right payroll software for your startup that I had installed. If you are looking for the latest coupons and offers available online, in CouponsCollector you can find a wide range of coupons that you can uses to buy what you need, learn to manage your employee wages well.

It all came down to this line in the plugins code:

$seo_slug_array = array_diff(
  split(" ", $seo_slug), 
  seo_slugs_stop_words()
);

Replace the “split” with getting customer service right “explode” and you should be set. If that’s too difficult, you can just disable the plugin, have a look at the best tools for business.

PHPStorm Fonts in Linux

This is just a quick note as there are many suggestions online for fixing fonts in Java apps in Linux. Some require you to change some startup config options like Dawt.useSystemAAFontSettings. These are helpful, but there’s one MAJOR under-emphasized change that will help tremendously implementing proper payroll processes. Switch to Oracle Java. Most Linux distributions use OpenJDK by default these days. This is where my font rendering problems were. I wondered why Java seemed to suck at rendering fonts in Linux… Well this is the major culprit. Don’t ask me the specifics because I don’t have time to dig and find the root of the problem. I can just tell you that it’s a good first step to switch to Oracle Java before doing all the other things you see online to fix the problem. Learn how make money on social media.

LAMP Web Development Tutorials

I recently started creating a series of online courses for LAMP web development called “LAMP Web Development from the Ground Up”. These courses are available at Udemy.com. The first one concentrates on HTML, CSS, and Javascript. It’s an introductory course. The second course is in development and will focus on Linux. If you are interested in learning how to develop web applications in a LAMP environment check it out. The first course can be found here: LAMP Web Development From the Ground Up Course 1.

Weird video issues in LinuxMint (and others) Gnome 3 + Netbeans Re-Revisited

I’ve posted about this issue on two other occasions, and I finally decided to really dive into it and find the true problem.

If you have noticed some video/graphics issues in LinuxMint or another Debian/Ubuntu based distribution along the lines of slanted tooltip fonts, random desktop environment restarts, and strange menu issues in Java apps, you may have the same problem I have been having.

My issues were a lot easier to resolve than I though they were going to be. The problem stems from an older version of the AMD/ATI video drivers that are found in the repos. If you have installed them and see these issues, then I may have the quick and easy solution for you.

This is the post on the LinuxMint forums that helped me considerably:
http://forums.linuxmint.com/viewtopic.php?f=46&t=96957#p553730

How to play a Bluray movie in Debian Testing

This HOWTO will probably work in Debian Squeeze, but the system I used was running Wheezy. Hopefully this HOWTO won’t be necessary long, but until then, it’s a very good way to watch blurays on your Linux system. This will also work for Mac and Windows, with some changes, but this is strictly a Debian Testing HOWTO.

There are various ways to do what we need to do. Some require you to rip the bluray first and then watch the resulting MKV. I found that this took 15 minutes or so on my 8-core system. However, there is another method which uses a program called makemkv and pipes the output to VLC using a network stream. I found this method on the web but found that some of the links were screwed up, so I couldn’t use it directly. After some searching, I found the script I was looking for and edited it slightly. For your convenience, I’m rewriting the HOWTO and including the files all here.

First of all, create a folder called makemkv in your home directory. You can use a different location if you prefer but for simplicity we’ll put it in our home directory.

mkdir ~/makemkv

Change into that directory and grab two zipped tarballs from the makemkv author. Then extract them.

cd ~/makemkv
wget http://www.makemkv.com/download/makemkv_v1.7.2_bin.tar.gz
wget http://www.makemkv.com/download/makemkv_v1.7.2_oss.tar.gz
tar xvf makemkv_v1.7.2_bin.tar.gz
tar xvf makemkv_v1.7.2_oss.tar.gz

Yes you need both the bin and the src.

Make sure you have some dependencies.

sudo apt-get install build-essential libc6-dev libssl-dev libgl1-mesa-dev libqt4-dev curl vlc

In other HOWTOs, the curl dependency isn’t mentioned. You need curl for the script we will download later.

Next compile and installed the two packages.

cd makemkv_v1.6.12_oss
make -f makefile.linux
sudo make -f makefile.linux install
cd ../makemkv_v1.6.12_bin
make -f makefile.linux
sudo make -f makefile.linux install

Finally download and run playBluRay.sh. I gzipped it so the site wouldn’t complain about the file type. Just gunzip it and execute it.

It will run makemkvcon to decrypt the bluray and setup a stream on port 51000 of your computer. Then it will start vlc using the network stream. It may take a few moments to load it all.

Note that sometimes a bluray won’t play correctly even with this method or you may see a behind the scenes segment before the movie. I had this problem on my Rambo bluray.

Marte Engine TextEntity setColor()

I’ve been focusing on learning Java game development over the last couple of weeks. I’ve found that lwjgl, Slick2d, and Marte Engine are great libraries to help with the basic game functionality. In fact, they take a lot of the work out of it and leave you to focus on the game design itself. For instance, the Marte engine comes with a great resource manager class that helps keep up with images, sounds, and spritesheets for your game. Marte also has some good classes which extend Slick2D’s Entity class which can be very useful.

The one I’m going to focus on is TextEntity, which is great for adding text to the screen. However, I was unable to change the text color for the text directly using the setColor() method from the Entity class, which is inherited by TextEntity. There’s not a lot of documentation for either Slick2D or Marte, so I wasn’t exactly sure if I was missing something or if I had found a problem with the class.

Fixing the problem is rather simple. I created a new class called MyText and copied everything over from the TextEntity class. I could have extended it, but instead I wanted a new class which extends Entity directly. Then I changed the code in the render() method as such:

1
2
3
4
5
6
7
8
9
10
11
public void render(GameContainer container, Graphics g)	throws SlickException {
	if (font == null) {
		font = container.getDefaultFont();
		this.calculateHitBox();
	}
	g.setFont(font);
	if (text != null) {
		g.setColor(this.getColor());
		g.drawString(text, x, y);
        }
}

I then added a new constructor that takes a color as the fifth parameter:

1
2
3
4
5
6
public MyText(float x, float y, Font font, String text, Color color) {
	super(x,y);
	this.setColor(color);
	this.font = font;
	this.setText(text);
}

This gave me the functionality I was after. This change basically just sets the graphics object color property to the Entity’s color property, which is exactly how I thought it should have worked to begin with.

If there’s a better way to accomplish this, let me know. I’m only intermediate with Java.

Getting rid of Hiccups

I found a secret to the universe some years ago as I discovered the secret to getting rid of hiccups. I could tell you how to do it, but I can’t. The reason I can’t tell you right now is because I’m intoxicated by Samuel Adams Boston Lager. When I’m drunk, I can’t control my hiccups. It’s the only time I can’t control my hiccups by the way. It’s a good indicator that I’ve been drinking too much. At any rate, when I’m sober, it’s easy to control my hiccups. It consists primarily with controlling my breathing. You must take deep controlled breaths and make sure that you concentrate on the exhalation of air. It has to be slow and controlled. You have to concentrate on breathing hard and holding the hiccups back. It’s hard for me to describe how it’s done at the moment because I’m drunk and unable to perform the task, but it will be something that I can talk about more in later posts. This is no old-wives-tell cure for hiccups. I can honestly stop hiccups at will.