At first glance, Wal-Mart Stores, Inc. (WMT 0.57%) and Whole Foods Market, Inc. (WFM) may not seem to have very much in common. You may think a high-end organic grocer might not be a suitable opponent for a big-box retailer. But just under the surface, the number of similarities is surprising. In each case there's another game in town, and each company has been losing share in its respective market. Both have much to lose in the ongoing trend toward e-commerce, and investors have been moving to the sidelines, waiting to see if either could up its game.

Wal-Mart and Whole Foods have lagged the broader market by a wide margin over the past several years. But investors can sometimes find a bargain in a beaten-down company. Let's look at the case for both to see which is the better buy now.

WMT Chart

Data by YCharts

E-commerce is eating Wal-Mart's lunch

Wal-Mart's biggest challenge has been the astounding rise of e-commerce, as more and more consumers opt for the convenience of online shopping. Another factor dragging on the company's results has been the persistent strength of the U.S. dollar, as approximately 25% of Wal-Mart's total revenue comes from international markets.

Wal-Mart responded to the online conundrum last year by buying e-commerce start-up Jet.com for $3.3 billion. The company has made several other acquisitions to better compete in the virtual marketplace, including its purchase of outdoor retailer Moosejaw and women's clothier ModCloth, one of the world's largest independent online fashion retailers. 

In its most recent financial release, Wal-Mart reported revenue that increased to $117.5 billion, up 1.4% over the prior-year quarter. Discounting foreign currency translations would have resulted in an increase of 2.5%, but that wasn't the headline. Investors cheered the company's year-over-year e-commerce growth of 63% and its 69% increase in online gross merchandise volume over the prior-year quarter. The icing on the cake was that this was the result of organic growth from Walmart.com, not as the result of acquisitions. The company also saw same-store-sales increase by 1.4% year over year. 

"We're moving faster to combine our digital and physical assets to make shopping simple and easy for customers," said Doug McMillon, Wal-Mart's president and CEO. 

A woman pushes a shopping cart through a grocery store, accompanied by two children.

Two retailers facing changing markets. Which should investors buy now? Image source: Getty Images.

Competition is weighing on Whole Foods

Whole Foods has been battling on numerous fronts as it seeks to lose the reputation for the premium pricing that gave it the nickname "whole paycheck." While it was once the only game in town, other grocers, including The Kroger Co. (KR -0.43%), saw the success the company was enjoying in natural and organic fare and responded by carrying competing items at lower prices. Even Wal-Mart entered the fray by stocking a selection of organic staples.

Expansion from direct competitors, including Sprouts Farmers Market, Inc. (SFM 0.47%) and privately held Trader Joe's, with their smaller-footprint stores, cut further into the natural-grocery market, while Whole Foods struggled to reorient its business. It has now seen seven consecutive quarters of same-store-sales declines.

Whole Foods has responded to these challenges by introducing its smaller concept store, 365 by Whole Foods Market, in an attempt to court millennial shoppers and value-conscious consumers, and to increase its presence in more urban areas. The company has rolled out four of these test stores and has been working to refine the concept. Whole Foods believes they're gaining traction and has plans to introduce 22 more.

Whole Foods recently overhauled its board of directors, hired a new CFO, and appointed a new board chairman. The company will also revamp its customer loyalty program and cut costs by $300 million, and it plans to increase returns to shareholders. The goal is to return to positive comps and earnings growth by fiscal 2018.

In its most recent quarter, Whole Foods saw sales of $3.7 billion, a 1.1% increase over the prior-year period, while same-store sales fell by 2.8%.

So which is the better buy?

On a price-to-earnings basis, Whole Foods trades at 28 times trailing earnings, while Wal-Mart is significantly less expensive at 18. Looking forward doesn't change things much. Wal-Mart's forward multiple of 18 times forward earnings is cheaper than Whole Foods' 27. Wal-Mart stock has performed better, yet Whole Foods still commands a premium valuation. Any failure to deliver on investors expectations, and Whole Foods' stock could be punished. 

WMT PE Ratio (TTM) Chart

PE Ratio (TTM) data by YCharts

Wal-Mart also has the edge in shareholder returns. Its share count has fallen by over 2% in the past year, while Whole Foods has remained the same. Wal-Mart's dividend currently yields 2.5%, to Whole Foods' 1.5%.

In the end, Wal-Mart has a better valuation and has returned more to shareholders, and its plans to better compete in an e-commerce world are already bearing fruit. Considering all these factors, Wal-Mart is the better buy.